Search Unity

MissingReferenceException:

Discussion in 'Scripting' started by TechHead404, May 16, 2019.

  1. TechHead404

    TechHead404

    Joined:
    Feb 25, 2019
    Posts:
    13
    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 <c8d2deaeeb3a4471b3e224c2a136823a>:0
    CoinSpawner.ShiftPipes () (at Assets/Scripts/CoinSpawner.cs:106)
    CoinSpawner.Update () (at Assets/Scripts/CoinSpawner.cs:81)

    Code (CSharp):
    1.    void Update()
    2.  
    3.     {
    4.  
    5.         if (game.GameOver) return;
    6.  
    7.         targetAspect = (float)targetAspectRatio.x / targetAspectRatio.y;
    8.         dynamicSpawnPos.x = (spawnPos.x * Camera.main.aspect) / targetAspect;
    9.  
    10.         spawnTimer += Time.deltaTime;
    11.         if (spawnTimer >= spawnRate)
    12.         {
    13.             SpawnPipe();
    14.             spawnTimer = 0;
    15.         }
    16.  
    17.  
    18.  
    19.         ShiftPipes();
    20.  
    21.     }
    22.  
    23.     void SpawnPipe()
    24.     {
    25.         GameObject pipe = Instantiate(PipePrefab) as GameObject;
    26.         pipe.transform.SetParent(transform);
    27.         pipe.transform.localPosition = dynamicSpawnPos;
    28.         if (beginInScreenCenter && pipes.Count == 0)
    29.         {
    30.             pipe.transform.localPosition = Vector3.zero;
    31.         }
    32.         float randomYPos = Random.Range(spawnHeight.min, spawnHeight.max);
    33.         pipe.transform.position += Vector3.up * randomYPos;
    34.         pipes.Add(pipe.transform);
    35.     }
    36.  
    37.     void ShiftPipes()
    38.  
    39.  
    40.     {
    41.         for (int i = pipes.Count - 1; i >= 0; i--)
    42.         {
    43.  
    44.             pipes[i].position -= Vector3.right * shiftSpeed * Time.deltaTime;
    45.             if (pipes[i].position.x < (-dynamicSpawnPos.x * Camera.main.aspect) / targetAspect)
    46.             {
    47.                 GameObject temp = pipes[i].gameObject;
    48.                 pipes.RemoveAt(i);
    49.                 Destroy(temp);
    50.             }
    51.         }
    52.     }
    53.  
     
  2. dgoyette

    dgoyette

    Joined:
    Jul 1, 2016
    Posts:
    4,196
    Most likely you're adding items to `pipes' that are later getting destroyed by something else, yet you're continuing to access them here in your ShiftPipes method. If you're going to do this sort of thing, you should just make sure that pipes isn't null as you're looping over the pipes. Ideally remove entries that are null from time to time.
     
  3. TechHead404

    TechHead404

    Joined:
    Feb 25, 2019
    Posts:
    13
    I fixed it just didn't update on here. I just destroy the sprite render and the collider. Might not be right but it works. Now I am on to new problems with google play services.
     
  4. TechHead404

    TechHead404

    Joined:
    Feb 25, 2019
    Posts:
    13
    Code (CSharp):
    1. {
    2.     private void OnTriggerEnter2D(Collider2D other)
    3.     {
    4.         {
    5.  
    6.             if (other.gameObject.tag == "Player")
    7.             {
    8.                 GetComponent<SpriteRenderer>().enabled = false;
    9.                 GetComponent<Collider2D>().enabled = false;
    10.             }
    11.  
    12.            
    13.         }
    14.     }
    15.  
    16. }