"Object reference not set to an instance of an object" unity2D,"Object reference not set to an instance of a object" Unity2D
Hi, I'm still a beginner in unity and I'm sure I'm making a very rookie mistake. But I just can't wrap my head around what I am doing wrong. Any help would be appreciated!
I want to make a script that clones platform a certain distance from each other at random variating heights. I also have a GameObject var that stores the last platform created, so I can just get the position of that and add it on. The problem that I am getting is that it clones it an infinite amount of times on the same spot instead of a few units away from it as it should.
This is what I got so far (It is only supposed to do it once for now):
public class PlatformSpawner : MonoBehaviour
{
public GameObject platform;
public float height;
public float distance;
private Transform lastPlatform;
private void Start()
{
spawnPlatfrom(distance);
lastPlatform = platform.transform;
}
private void spawnPlatfrom(float distanceFrom)
{
if (PlayerCollision.stuckOnWall == false)
{
GameObject newPlatform = Instantiate(platform);
newPlatform.transform.position = new Vector3(lastPlatform.position.x + distance, Random.Range(-height, height), 0);
lastPlatform = newPlatform.transform;
Destroy(newPlatform, 10);
}
}
Here is the full error by the way:
NullReferenceException: Object reference not set to an instance of an object PlatformSpawner.spawnPlatfrom(System.Single distanceFrom) (at Assets/Scripts/PlatformSpawner.cs:23) PlatformSpawner.Start() (at Assets/Scripts/PlatformSpawner.cs:13)