Been fighting this for a few days. I want the Coin to be collected with the player touches it. I thought I could copy my script from the other objects and just use it for the coins. So that is why it says pipe instead of coin.
I just started coding 2 months ago so this could be a complete noob problem.
MissingReferenceException: The object of type ‘Transform’ has been destroyed but you are still trying to access it.
Your script should either check if it is null or you should not destroy the object.
UnityEngine.Transform.get_position () <0x34a0a450 + 0x0006a> in :0
CoinSpawner.ShiftPipes () (at Assets/Scripts/CoinSpawner.cs:106)
CoinSpawner.Update () (at Assets/Scripts/CoinSpawner.cs:81)
void Update()
{
if (game.GameOver) return;
targetAspect = (float)targetAspectRatio.x / targetAspectRatio.y;
dynamicSpawnPos.x = (spawnPos.x * Camera.main.aspect) / targetAspect;
spawnTimer += Time.deltaTime;
if (spawnTimer >= spawnRate)
{
SpawnPipe();
spawnTimer = 0;
}
ShiftPipes();
}
void SpawnPipe()
{
GameObject pipe = Instantiate(PipePrefab) as GameObject;
pipe.transform.SetParent(transform);
pipe.transform.localPosition = dynamicSpawnPos;
if (beginInScreenCenter && pipes.Count == 0)
{
pipe.transform.localPosition = Vector3.zero;
}
float randomYPos = Random.Range(spawnHeight.min, spawnHeight.max);
pipe.transform.position += Vector3.up * randomYPos;
pipes.Add(pipe.transform);
}
void ShiftPipes()
{
for (int i = pipes.Count - 1; i >= 0; i--)
{
pipes[i].position -= Vector3.right * shiftSpeed * Time.deltaTime;
if (pipes[i].position.x < (-dynamicSpawnPos.x * Camera.main.aspect) / targetAspect)
{
GameObject temp = pipes[i].gameObject;
pipes.RemoveAt(i);
Destroy(temp);
}
}
}