Search Unity

Can I reference a specific GameObject in an array?

Discussion in 'Scripting' started by williamsmd90, Jan 18, 2019.

  1. williamsmd90

    williamsmd90

    Joined:
    Sep 3, 2018
    Posts:
    22
    I've been trying for the longest to figure out how to add multiple powerups to a scene and elicit a different action based on the powerup that's collected. I wish I had saved the code that I've already tried, but everything I've tried has failed. Currently, picking up any of the powerups gives me the same response as picking up the first one in the array. For instance, the following code only gives me "Rapid Fire" in the console when I want it to show me "Rapid Fire" for the first one and "Invincibility" for the second:

    Code (CSharp):
    1.     SpawnPowerups spawnPowerups;
    2.    
    3.     private void Start()
    4.     {
    5.         spawnPowerups = GetComponent<SpawnPowerups>();
    6.     }
    7.    
    8.     private void OnTriggerEnter(Collider other)
    9.     {
    10.         if(other.CompareTag("Powerup"))
    11.         {
    12.             if(other == spawnPowerups.powerups[0])
    13.                 Debug.Log(other);
    14.             else if(other == spawnPowerups.powerups[1])
    15.                 Debug.Log(other)
    16.         }
    17.     }
     
  2. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    Are you sure your second powerup's gameobject is actually named "Invincibility", it is the same reference as powerups[1], and has the correct tag? You should add more debugging, like where you are filling out this array, what the names of gameobjects are that trigger OnTriggerEnter that don't match an element of the array, etc.