Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Cant collect power ups after my player ship dies and is respawned

Discussion in '2D' started by Stew_79, Apr 2, 2021.

  1. Stew_79

    Stew_79

    Joined:
    Feb 24, 2021
    Posts:
    36
    Hi guys
    I've hit a problem in my game that I cant get my head around.
    If my Player ship is killed in the game and I respawn him with my game controller script I can no longer access my power up counter in the player controller script.
    It all works perfect before he is killed.
     

    Attached Files:

    Last edited: Apr 2, 2021
  2. Derekloffin

    Derekloffin

    Joined:
    Mar 14, 2018
    Posts:
    322
    Can't really tell what's going on here. I don't see a power up counter here so not sure which thing you're referring to. And, what exact error message are you getting when you encounter the issue? It should display on the console.
     
  3. Stew_79

    Stew_79

    Joined:
    Feb 24, 2021
    Posts:
    36
    Hi Yeah sorry my code not the neatest
    but in the player controller script there is a power up counter that basically gets incremented every time I pick up a power up.
    I then use that to select weapons on the the weapons select bar at the bottom of the screen.
    The error I am getting after I respawn the the player ship is

    NullReferenceException: Object reference not set to an instance of an object
    WeaponsBar.Update () (at Assets/Scripts/WeaponsBar.cs:33)

    it points to this line in my weapons bar

      if (playerScript.powerUp >= 1 && t <= UItext.Length)
     
  4. Zephus

    Zephus

    Joined:
    May 25, 2015
    Posts:
    356
    You need to refactor your entire code completely. Rule number one when working with Unity: Don't call FindObjectOfType or GetComponent or anything like that in Update. You couldn't get more inefficient if you tried. You're looking through your entire Scene in every single Update, don't do that.

    Just have a reference to your player in your GameController and use that. But that's not where the error comes from - you check in WeaponsBar.cs if PlayerAlive in the GameController is false. So if he's dead, you try to find the new Player_Controller (by the way, you should remove that underscore, that's against convention). I can't find any line where you actually set PlayerAlive to true again, so I'll assume you do that somewhere else? If you do, that means
    • Everything works
    • Player dies -> PlayerAlive = false, so WeaponsBar starts looking for new Player_Controller
    • PlayerAlive = true, so WeaponsBar stops looking, which means WeaponsBar will never find the Player
    Which is why you should stop fragmenting your code so much and using so many FindObjectOfType calls. Get rid of that entire Update loop in WeaponsBar and only change the text when certain events happen. WeaponsBar shouldn't be doing all of this in every frame, that's extremely slow.
     
    Stew_79 likes this.
  5. Stew_79

    Stew_79

    Joined:
    Feb 24, 2021
    Posts:
    36
    Thanks @Zephus
    I have completely done away with the weapons bar script and implemented it in PlayerController
    Everything is working again Thanks mate.
     
  6. Stew_79

    Stew_79

    Joined:
    Feb 24, 2021
    Posts:
    36
    That feeling You get when you spent ages fixing 1 bug only to find another! But thats learning for ya ;)