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 JonWag · Jun 24, 2013 at 06:11 PM · scritping

What is NullReferenceException :Object reference not set to an instance of an object?

What is this error and why do i keep getting it? I am very new to coding please help. Here is my code :

 #pragma strict
 //Player Script
 
 //Inspector Variables
 var tagName             : String;
 
 var rayDistance         : float = 100.0 ;  //length of ray for the raycast
 
 var score               : int   = 0;       // Score for the player
 
 var gameTime            : float = 20;       //The time the game will last 
 
 var loadWaitTime        : float = 1.0;      //Amount of time to wait before next scene is loaded
 
 var numberOfPointsToWin : int   = 5;
 
 //Private Variables
 
 
 function Start()
 {
   InvokeRepeating("CountDown", 1.0, 1.0);        //Repeat countdown every second
 }
 
 //Update is called every frame
 function Update () 
 {
 
   
   if(Input.GetMouseButtonDown(0))
  {  
     
      var hit : RaycastHit;
      
      var ray : Ray =Camera.main.ScreenPointToRay(Input.mousePosition); //gets mouse position 
      
      if(Physics.Raycast(ray, hit, 100.0))
     
      {
             if (hit.transform.tag == tagName) {
                 
                 var enemyScript = hit.transform.GetComponent(ScriptActorEnemy);
                                 
                 enemyScript.numberOfClicks -= 1;                       //Reduce the number each click
                 
                 //Checks if object is at 0 for adding points to the score
                 if(enemyScript.numberOfClicks == 0)
                 {
                    score += enemyScript.enemyPoint;                    //adds points to overall score
                 } 
                 
                             
      }
     }
   }   
 }
 
 
 function CountDown()
 {    if(--gameTime == 0)  //Subtract from gameTime
     {
       CancelInvoke("CountDown");  //Cancel CountDown at 0
       if(score >= numberOfPointsToWin)
       {
           Application.LoadLevel("sceneScreenWin");
       }
       else
       {
         Application.LoadLevel("sceneScreenLose");
       }
     }
     
 }
 //
 function OnGUI()
 {
     GUI.Label(Rect(10,10,100,20),"Score: "+ score);
     GUI.Label(Rect(10,25,100,35),"Time: "+ gameTime);
 }
Comment
Add comment · Show 5
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 brandonsbarber · Jun 24, 2013 at 06:14 PM 0
Share

What part of the code is causing the error? Try to only post your relevant code in the future.

avatar image Em3rgency · Jun 24, 2013 at 06:15 PM 0
Share

It means you're trying to access an empty object. Either you didn't assign it correctly, or you're trying to access it before its assigned. Which line is throwing the error?

avatar image JonWag · Jun 24, 2013 at 06:22 PM 0
Share

Thank you for the fast answers! The line is 44.

avatar image Em3rgency · Jun 24, 2013 at 06:24 PM 0
Share

@jonWag Yes, its as I thought. the enemyScript variable is not getting assigned. Check my answer, see if that helps it?

avatar image Em3rgency · Jun 24, 2013 at 06:28 PM 0
Share

If the answer helped, don't forget to mark it as correct :)

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by Em3rgency · Jun 24, 2013 at 06:20 PM

 var enemyScript = hit.transform.GetComponent(ScriptActorEnemy);

should be

 var enemyScript : ScriptActorEnemy = hit.transform.GetComponent(ScriptActorEnemy);

http://docs.unity3d.com/Documentation/ScriptReference/GameObject.GetComponent.html

EDIT: Woops, typos. Corrected.

Comment
Add comment · Show 6 · 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 JonWag · Jun 24, 2013 at 06:31 PM 0
Share

The error is still occurring after I fixed the code. It still says thee problem is line 44. Any suggestions?

avatar image Em3rgency · Jun 24, 2013 at 06:35 PM 0
Share
  if (hit.transform.tag == tagName)

I don't see you actually setting the tagName anywhere. Do you do it in the inspector or what? Because otherwise, if it just defaults to the hits tagname (I don't see any other way it would pass that if check), that would mean ANYTHING the ray hits would try to get the component ScriptActorEnemy. And if not everything has it, your variable would not get set properly.

avatar image JonWag · Jun 24, 2013 at 06:39 PM 0
Share

Yea the tagName is set in the inspector.

avatar image Em3rgency · Jun 24, 2013 at 06:43 PM 0
Share

Well as a last resort, all I can think of is

        if (hit.transform.tag == tagName) {
  
                 var enemyScript = hit.transform.GetComponent(ScriptActorEnemy);
                 if(enemyScript != null)
                 {
                     enemyScript.numberOfClicks -= 1;                        //Reduce the number each click
  
                 //Checks if object is at 0 for adding points to the score
                     if(enemyScript.numberOfClicks == 0)
                     {
                        score += enemyScript.enemyPoint;                    //adds points to overall score
                     } 
                 }
          }
avatar image fafase · Jun 24, 2013 at 07:29 PM 0
Share

Your answer is not fixing the issue. UnityScript does not require any static typing.

Your two lines in the answer do exactly the same thing.

As for the problem, is there an actual script attached to the object? Is it on the same object and not down in the hierarchy of the object?

Show more comments

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

16 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

Related Questions

Creating piping model at runtime 0 Answers

Arkanoid clone: How can I enable/disable colliders in my prefabs while the game runs? 1 Answer

Is there a way I can access and edit the Character Controller component's script? 1 Answer

need help on prefab spawning 1 Answer

Unity Inspector Choose C# object from list 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