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 /
avatar image
0
Question by Daniel-Everland · Oct 21, 2012 at 05:49 PM · c#errorreferencenullexception

Despawner script returns null reference exception.

I'm currenly developing a 2D game in which you're to avoid some blocks which falls down. Do have them despawn after they leave the screen, I've created a cube that despawns anything that touches is, and that used to work perfectly.

That is until I added a scorepoint system. Basically, I want it to at 10 points to the score everytime a block despawns, as in everytime you dodge a cube, and that worked, but if you did hit a cube and died it just kept on counting. So I made a script which should check if you're alive or not, and stop the count of cubes if you're not, but I get a null reference exception when a cube hits my despawner. Heres my despawner script and my player movement script.

PlayerMovementScript

 using UnityEngine;
 using System.Collections;
 
 public class PlayerMovementScript : MonoBehaviour {
     public float Speed = 2.0f;
     public int Lives = 3;
     public GameObject Player;
     public bool CheckNumberOfShipsAlive = false;
         
     
     void FixedUpdate()
     {
         if (Input.GetKey(KeyCode.D))
         {
             transform.position += transform.forward * Speed * Time.deltaTime;
         }
         if (Input.GetKey(KeyCode.A))
         {
             transform.position -= transform.forward * Speed * Time.deltaTime;
         }
         if (Input.GetKey(KeyCode.R))
         {
             if (CheckNumberOfShipsAlive = true)
             {
                 Debug.Log("Respawn!");
                 GameObject spawnPlayer = (GameObject)Instantiate (Player);
                 CheckNumberOfShipsAlive = false;
             }
             else
             {
                 return;
             }
         }
     }
     void OnTriggerEnter()
     {
         Destroy(this.gameObject);
         Lives -= 1;
         CheckNumberOfShipsAlive = true;
     }
 }


DespawnerScript

 using UnityEngine;
 using System.Collections;
 
 public class DespawnerScript : MonoBehaviour {
     public float Score = 0.0f;
 
     void OnTriggerEnter(Collider other)
     {
         GameObject score = GameObject.Find("Player");
         PlayerMovementScript playerMovementScript;
         playerMovementScript = GetComponent<PlayerMovementScript>();
         if (playerMovementScript.CheckNumberOfShipsAlive = false)
         {
             Destroy(other.gameObject);
             Score += 10.0f;
             Debug.Log("Your score is " + Score);
         }
         else
         {
             return;
         }
     }
 }
Comment
Add comment · Show 4
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 FlyingOstriche · Oct 21, 2012 at 06:27 PM 0
Share

what line is error on

avatar image Daniel-Everland · Oct 21, 2012 at 06:29 PM 0
Share

Oh sorry, it's on line 12 in the DespawnerScript :)

avatar image FlyingOstriche · Oct 21, 2012 at 06:35 PM 0
Share

Should be "if (player$$anonymous$$ovementScript.CheckNumberOfShipsAlive == false)"

Although that probably isn't causing the error.

In most languages if( variable=something) just sets the variable to something and checks if variable is true.

To get that error though player$$anonymous$$ovementScript is probably null. Try "Debug.Log(player$$anonymous$$ovementScript );" after "player$$anonymous$$ovementScript = GetComponent();"

avatar image phodges · Oct 21, 2012 at 07:01 PM 0
Share

All of the above, plus judging from your DespawnerScript and it's use of GetComponent, you have a Player$$anonymous$$ovementScript attached to the same object; both react to OnTriggerEnter. Since the Player$$anonymous$$ovementScript reacts to this event by destroying its GameObject (and therefore itself), it seems fair to imagine that nothing good can come of this.

On the other hand, if that GetComponent is written in error then you should expect the return value to be null and to see the exception you describe.

0 Replies

· Add your reply
  • Sort: 

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

11 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

Related Questions

NullReferenceException Error 6 Answers

Instantiate() as GameObject = null reference 1 Answer

Null Reference Exception error 3 Answers

NullReferenceException - With Network PlayerSpawn 0 Answers

NullReferenceException: object not in inspector? 0 Answers


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