Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
This post has been wikified, any user with enough reputation can edit it.
avatar image
0
Question by Kantus · Feb 10, 2015 at 09:46 PM · delaynullrespawn

Dealing with nulls on Respawn

Hi all,

I have a GUI script that calls upon the Player object in order to populate the Health and ammo counters. When my player dies, there is a two-to-three second delay before they are respawned. However, this means that (since I am still using onGUI() for my simple GUI boxes), I get an error, since the Player no longer exists, has not yet been instantiated, and thus cannot have its Scripts or attributes (including Health, ammo, etc) read off from it. The relevant code is here:

     void OnGUI(){
                 //making sure game doesn't crash if if there's nulls
                 if (Theplayer == null) {
                 Theplayer = GameObject.FindGameObjectWithTag("Player");
                 }
                 if (playersArm == null) {
                 playersArm = Theplayer.transform.FindChild("Arm");
                 }
                 if (musket == null) {
                 musket = playersArm.FindChild ("Musket").GetComponent<Musket> ();
                 }
                 if (crossbow == null) {
                 crossbow = playersArm.FindChild("Crossbow").GetComponent<Crossbow> ();        
                 }
                 musketBallStock = musket.ammo.ToString();
                 crossbowBoltStock = crossbow.ammo.ToString();
 
 
 
 
                 //Drawing the ammo boxes
                 if (crossbow.active && crossbow.enabled && crossbow!= null) {
                         GUI.Box (new Rect (0, Screen.height - 50, 100, 50), crossbowBoltStock);        
                 } else if (musket.active && musket.enabled && musket != null) {
                         GUI.Box (new Rect (0, Screen.height - 50, 100, 50), musketBallStock);
                 } else {
                  GUI.Box (new Rect (0, Screen.height - 50, 100, 50), "");
                 }
         
                 //Drawing Health Box
         GUI.Box (new Rect (0, Screen.height - 100, 100, 50), Theplayer.GetComponent<Player>().playerStats.Health.ToString());
                 }


and, in case it's relevant, here's the respawn code (in a totally different script, but it might still be of use):

     public IEnumerator respawnPlayer(){//Notice that the return type is no longer void, but IEnumerator, since we YIELD a new WaitForSeconds object
         audio.Play ();
         yield return new WaitForSeconds (spawnDelay);
         Instantiate (playerPrefab, spawnPoint.position, spawnPoint.rotation);
         GameObject clone = Instantiate (spawnPrefab, spawnPoint.position, spawnPoint.rotation) as GameObject;


         Destroy (clone, 3f); //This line destroys the particles after 3 seconds. They fade out anyway, but they're still technically there and could cause problems if the project gets complex.
         //need to re-set the "target" attribute in EnemyAI.cs at this point, or it will crash because the Player it was assigned to chase will have been deleted
     }


Does anyone have any ideas as to how I might prevent this conflict from occurring?

Thanks

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by Alanisaac · Feb 11, 2015 at 01:53 AM

Since your player doesn't exist during this time, I assume any code dependent on the player existing doesn't matter. For example, between death and respawn, could we say it doesn't matter what the health bar looks like?

If that's the case, try exiting your function if you can't find the player object. Something like this might work:

 // ...
 //making sure game doesn't crash if if there's nulls
 if (Theplayer == null) 
 {
     Theplayer = GameObject.FindGameObjectWithTag("Player");
     if(Theplayer == null)
     {
         // Could not find player, do nothing else
         return;
     }
 }
 // ...

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Kantus · Feb 13, 2015 at 07:58 PM 0
Share

Thanks, that worked!

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

20 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Respawning a player after a certain amount of time 0 Answers

How to make my espawn delay trigger to work more than once. 2 Answers

How to make an enemy respawn? 0 Answers

Respawn Delay Not Working [Closed] 1 Answer

how to respawn with a simple delay? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges