So I’ve been trying to alter the contents of my “MoveRight” script from my “SurfaceDeathScript”, specifically I want to alter the value of a variable, and I know I’m supposed to use the “GetComponent” thing to accomplish this. I’ve looked up several sources, but for some reason I can’t seem to get it right. I’m using C# btw.
public class SurfaceDeathScript : MonoBehaviour
{
public MoveRight moveRightScript;
void Start ()
{
moveRightScript = gameObject.GetComponent<MoveRight>();
I’ve tried this, but I get “the name MoveRight does not exist in the current context”. I’ve also tried a whole manner of different ways, but to no avail. Any help would be appreciated, thank you.
EDIT:
Actually, I managed to write this and it worked:
public class SurfaceDeathScript : MonoBehaviour
{
public GameObject LevelScroller;
public MoveRight moveRightScript;
void Start ()
{
moveRightScript = LevelScroller.GetComponent<MoveRight>();
}
void OnTriggerEnter2D(Collider2D other)
{
if (other.gameObject.tag == "Player")
{
moveRightScript.levelscrollSpeed = 0.04f;
}
}
}
In unity I dragged the LevelScroller object into both the LevelScroller and MoveRightScript boxes, as it contains the MoveRight script, while this does work, I get an error at the start that says “UnassignedReferenceException: the variable LevelScroller of SurfaceDeathScript has not been assigned”.