Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 Dekanite · Nov 01, 2015 at 01:31 PM · c#errorobject-reference-error

Object reference not set to an instance of an object

I get this error whenever I press 5: NullReferenceException:

Object reference not set to an instance of an object WarriorSpin.Update () (at Assets/Scripts/SpellsAndAbilities/Warrior/WarriorSpin.cs:30)

I have checked to make sure everything is the way it is supposed to be but it still doesn't work.

Here is my code:

 using UnityEngine;
 using UnityEngine.UI;
 using System.Collections;
 
 public class WarriorSpin : MonoBehaviour {
 
     public GameObject player;
     
     public float coolDown;
     
     PlayerScript ps;
 
     public GameObject canvas;
     
     public GameObject errorMessageArea;
     
     void Start () {
         player = gameObject;
         
         ps = player.GetComponent<PlayerScript> ();
 
         canvas = GameObject.Find ("HudCanvas");
         
         errorMessageArea = GameObject.Find ("ErrorMessageArea");
     }
     
     void Update () {
         if (Input.GetButtonDown ("5")) {
             if (coolDown <= 0) {
                 if (ps.mana > 50) {
                     ps.isWarriorSpin = true;
                     coolDown = 30;
                     ps.mana -= 50;
                 } else {
                     GameObject notEnoughManaObject = Instantiate (ps.notEnoughMana, errorMessageArea.transform.position, errorMessageArea.transform.rotation) as GameObject;
                     notEnoughManaObject.transform.SetParent (canvas.gameObject.transform);
                 }
             } else {
                 GameObject notReadyYetObject = Instantiate (ps.notReadyYet, errorMessageArea.transform.position, errorMessageArea.transform.rotation) as GameObject;
                 notReadyYetObject.transform.SetParent (canvas.gameObject.transform);
             }
         }
 
         if (GetComponent<Text> () != null && coolDown > 1) {
             GetComponent<Text> ().text = "" + coolDown.ToString ("0");
         }
 
         if (GetComponent<Text> () != null && coolDown > 0 && coolDown <= 1) {
             GetComponent<Text> ().text = "" + coolDown.ToString ("F1");
         }
 
         if (GetComponent<Text> () != null && coolDown <= 0) {
             GetComponent<Text> ().text = "";
         }
         
         coolDown -= Time.deltaTime;
     }
 }
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

Answer by OncaLupe · Nov 01, 2015 at 05:18 PM

As long as the numbering matches up, the error is from the line if (ps.mana > 50) {. This generally means the variable 'ps' has not been assigned an object to point to. The Start() method tries to set this on line 20, but you never confirm if it was successful. If the object set to the 'player' variable does not contain 'PlayerScript', then 'ps' will be null and give the error.

Also, it looks like you're Instantiating an error message object and attaching it to the UI Canvas. Instantiating is an expensive operation. It'd be much better to already have a generic error message display on the UI, and call a method on it to display whatever error comes up.

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 Dekanite · Nov 01, 2015 at 06:39 PM 0
Share

I tested if ps was set by making it public for a second and looked in the inspector. It said that ps was set to the PlayerScript of the gameobject. So ps is set and that's why I'm confused. If it is set then why is it giving me this error?

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Distribute terrain in zones 3 Answers

Object reference not set to an instance of an object in c# class. 1 Answer

Multiple Cars not working 1 Answer

Particle circle HELP 1 Answer

Internal Compiler Error - Lidgren 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