I’ve been stuck on this for hours, i’m making an endless runner game and i’m trying to make the ground and obstacles move faster with time, i already made the ground move faster by increasing its float every 2.5 seconds but i can’t do that to an instantiated object because it gets deleted and when it respawns it’s float resets too, I tried using GetComponent<FloorScript>().floorSpeed; but it doesn’t seem to work, i also got an error that says "
NullReferenceException: Object reference not set to an instance of an object
MovingObjects.Update ()
"
GetComponent() finds only an object that is on the same object as whatever is currently running that code.
If you want to find an object no matter where it is, you could use FindObjectOfType(). If there are multiple FloorScripts (which I suspect is the case), you could use FindObjectsOfType and loop through that result. Be aware that both of these perform pretty badly.
If you want a variable that will affect the speed of all of your floor objects, it would probably be easier to use a static variable. This will be faster and easier than the above options.
// in FoorScript
public float floorSpeed = 1f;
// elsewhere
FloorScript.floorSpeed = 4f;
1 Like
it works, thank you so much