spagoni
spagoni

Reputation: 43

Why do my powerup prefabs work, but the instantiated clones do not?

This is the code that I have on my player script:

  void OnTriggerEnter2D (Collider2D collider2D)
     {
         if (collider2D.gameObject.tag == "powerup")
         {
             PoweredUp = true;
             PowerupTimer = PowerupTimer;
             PowerupTimer -= Time.deltaTime;
         }
     }

...and this is on the powerup itself:

 void OnTriggerEnter2D(Collider2D collider2D)
 {
     if (collider2D.gameObject.tag == "Player")
     {
         Destroy(gameObject);
     }
 }

For some reason, any prefabs that I drop directly into my scene work perfectly, however instantiated ones get destroyed, but for some reason, do not trigger the "PoweredUp" boolean.

Here is a more complicated script that I have on the spawner objects:

  public GameObject powerup;
     public float minWait;
     public float maxWait;
 
     private bool isSpawning;
 
     IEnumerator SpawnObject(float seconds)
     {
         Debug.Log("Waiting for " + seconds + " seconds");
 
         yield return new WaitForSeconds(seconds);
         Instantiate(powerup, transform.position, transform.rotation);    
         isSpawning = false;
     }
 
     void Update()
     {
         if (!isSpawning)
         {
             isSpawning = true;
             StartCoroutine(SpawnObject(UnityEngine.Random.Range(minWait, maxWait)));
         }

Any solutions, or ways I could have done this more efficiently?

UPDATE: I found out that the script on the Player object is running fine, but the boolean still isn't changing.

Upvotes: 0

Views: 31

Answers (1)

spagoni
spagoni

Reputation: 43

Ok I got it, there was a compiler warning referencing the line "PowerupTimer = PowerupTimer" so I just changed it to "PowerupTimer = 7f" which is pretty much the same thing.

Upvotes: 1

Related Questions