How do I load a script externally? As in from streaming assets? I would like to make changes to the code. For example a simple code like this
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class SetValue: MonoBehaviour
{
private int value = 12;
// Start is called before the first frame update
void Start()
{
}
}
Drag the .cs file into the project.
Related
How do I fix this it was working before but it broke somehow: (Solved)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class SM_SceneSwitcher : MonoBehaviour {
public void playGame()
{
SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex + 1);
}
public void Back()
{
SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex - 1);
}
I guess your script is not attached to the game object anymore ?
This happens if you rename or move your script, your button still references the old path, you need to put your script again in your button
This, or the playGame and Back functions are not assigned to the click action.
This question already has answers here:
How to access a variable from another script in another gameobject through GetComponent?
(3 answers)
Closed 2 years ago.
so, my code;
Script one;
using System.Collections.Generic;
using UnityEngine;
public class mousePosTracker : MonoBehaviour
{
public Vector2 mousePos;
public void Update()
{
mousePos = Input.mousePosition;
Debug.Log(mousePos);
}
}
and script two;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class player : MonoBehaviour
{
void Update()
{
transform.Translate(// mouse position);
}
}
the code currently is pretty bare-bones, but I will fix that. so what I want to do is; I want to access the vector2 mousePos variable in script2, so that I can move the player based on the mouse position.
Make a reference on your Player script to your MousePosTrackerScript and use it.
One recommendation, use CamelCase on your class names.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Player : MonoBehaviour
{
public MouseousePosTracker posTracker = null; //attach it from your Editor or via script
void Update()
{
transform.Translate(posTracker.mousePos);
}
}
One easy way is to use the Find method.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Player : MonoBehaviour
{
private MousePosTracker mousePosTracker;
void Start(){
mousePosTracker = GameObject.Find("gameobject name which has the MousePosTrackerScript").GetComponent<MousePosTracker>()
}
void Update()
{
transform.Translate(mousePosTracker.mousePos);
}
}
If the gameobject has a tag you can use the FindWithTag method too.
Do like this in your 2nd script, always make sure your script has first letter in caps.
EDIT: if you do not want to give reference (drag&drop) in editor, use the below example. If you want it to be simpler without providing reference in the script, use Lotan's solution.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Player : MonoBehaviour
{
private MousePosTracker mouse;
void Start()
{
mouse = (MousePosTracker) GameObject.FindObjectOfType(typeof(MousePosTracker));
}
void Update()
{
transform.Translate(mouse.mousePos);
}
}
There is a chance this is a duplicate, but I have tried and tried to get this to work and it's probably something simple.
I'm trying to get two scripts to interact on one object. I would like a basic generic physics script that will handle all the interactions for all my objects.
Script 1:
// Simple Player1
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Player1 : MonoBehaviour
{
Physics physics_script;
void Start(){
physics_script = gameObject.GetComponent<Physics>();
}
// Update is called once per frame
void Update() {
physics_script.helloWorldFromAnotherScript();
}
}
Script 2:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Physics : MonoBehaviour
{
void Start(){
}
void Update() {
}
public void helloWorldFromAnotherScript()
{
Debug.Log("Hello world from player");
}
}
Edit:
I'm pretty sure the problem is here:
But I can't change it to save my life.
It should work.
Are you sure you have BOTH scripts added to the gameObject? At the image I only see player1, and I doubt if Physics it's added.
Elsewhere you can make physics_scripts public or serialize them(with [SerializeField]) and you will be able to drag'n'drop at the editor the script to the formfield. Thats allows you to not use the GetComponent<> at start.
Remember to delete start and update methods if you dont use them.
I'm getting started with scripting in Unity and after running the attached codes. i get the error in the unity console. Any assistance for me.
Error in unity image.
This the the code i run in the VS for the unity.
enter code here
**using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CubeScript : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
Debug.Log("Start method is Called");
}
// Update is called once per frame
void Update()
{
Debug.Log("Update is calling");
}
}**
you have 2 Classes named CubeScript in your project. its the reason of error
Look at other script if one contains the same name of class
to avoid duplicate classes, you could add namespace too:
namespace NameOfSceneForExample
{
public class CubeScript : MonoBehaviour
{
}
}
I'm working on 2D Unity game and I would like to know how we can display an image on my inventory when the player collide with an object. For exemple, if the player collide with a drumstick, the drumstick image appear on the inventory menu.
Best Regards.
Yacine TAZDAIT.
Method #1
There are many different ways to display an image. For example, you can have an image with an image component, and you switch the component on and off whenever you want the image to appear/disappear. You can do this using code similar to this:
using UnityEngine;
using System.Collections;
using UnityEngine.UI; // Required when Using UI elements.
public class Example : MonoBehaviour
{
public Image drumstick;
public void Start()
{
toggleDrumstick(); // This will toggle the drumstick. For example, if the drumstick is not being shown at the time, the drumstick will show on the screen. The opposite is true.
}
public void toggleDrumstick() {
drumstick.enabled = !drumstick.enabled;
}
}
Method #2
The code above is a great solution, but there is a more modular way to do it.
using UnityEngine;
using System.Collections;
using UnityEngine.UI; // Required when Using UI elements.
public class Drumstick : MonoBehaviour
{
public static bool enabled = this.image.enabled;
}
I recommend the method above. The reason for this is because every script can now access the drumstick's status. For example, your player script can do this.
using UnityEngine;
using System.Collections;
public class Player : MonoBehaviour
{
void doSomething () {
Drumstick.enabled = true; // make the image appear.
}
}
For any of these methods to work, make sure your drumstick uses the image component.
EDIT:
To answer your question further, here is a way to implement method #2 in your code. In your player script, you can use OnCollisionEnter and the method above to make the drumstick appear.
using UnityEngine;
using System.Collections;
public class Player : MonoBehaviour
{
void OnCollisionEnter (Collision collision)
{
if (collision.gameObject.tag == "drumstick") Drumstick.enabled = false;
}
}
For this to work, make sure that the drumstick has the tag "drumstick".