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 XxCelosiaxX · Feb 17, 2014 at 03:38 AM · beginnernull reference exception

Beginner please help. NullReferenceException: Object reference not set to an instance of an object

I'm new at this whole coding thing so I don't know what is wrong here I'm making a simple space shooter game and this is the part where the player life suppose to subtract every time the astriod hit the player
here is my code:

  //Inspector variables
  var astroidSpeed : float = 10.0;
  var explosion    : Transform;
 
  //Private Variable
 
  // Game loop 
  function Update ()
 {
    transform.Translate(Vector3.down*astroidSpeed*Time.deltaTime); // vector3.down = move object down on y axis
   
   // make astriod re appear at the top when it hit the bottom
   if(transform.position.y <= -10)
   {
     // reset enermy position
     transform.position.y = 0;
     transform.position.x = Random.Range(-10.0,10.0);
   }
 }
 
 function OnTriggerEnter (other : Collider)
 {
     if(other.gameObject.tag == "Player") 
     {
       other.GetComponent("script player").lives -= 1;  // get component to get component of other source 
       Instantiate(explosion,transform.position,transform.rotation);
       
       // Reset enemy position
       other.transform.position.y = 10.0;
       other.transform.position.x = Random.Range(-10,10);
     }
 }

it seem that the code it correct because unity let me run it but instead of subtracting player life i get this error code on the console instead. can someone tell me why?

Comment
Add comment · Show 1
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 Olgo · Feb 17, 2014 at 04:54 AM 1
Share

what line is the error showing up on?

3 Replies

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

Answer by XxCelosiaxX · Feb 18, 2014 at 10:17 PM

Well I don't know what went wrong but I re type the whole thing today and it work now ._.

but thank you everyone for thanking to help. you guys are awesome!

Comment
Add comment · 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
0

Answer by zee_ola05 · Feb 17, 2014 at 05:00 AM

I'm guessing this returns null:

 other.GetComponent("script player")

There shouldn't be spaces on classnames.

Comment
Add comment · 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
0

Answer by FuzzyLogic · Feb 17, 2014 at 05:48 AM

Without running the code or knowing the exact line of the error, the only object variable being used that is not defined by Unity itself is the reference to the "script player" component.

Three things:

1) It is not possible to have an object name with a space in it. Your 'script player' component is certainly not named 'script player' or Unity is ignoring the script because it has an invalid name. If you have named the file as 'script player' then rename it.

To encourage best practices, all of your class names should be capitalized at each word with words packed together. eg. 'ScriptPlayer'

This is called CamelCase and should be used for naming all your object classes (pretty much every script filename).

For variable names, use lowerCase on the first word. If you follow this convention, it will be easier to read and understand your code because all class names will start with upper-case and all variable names will start with lower-case.

2) Make sure the object that is being collided with has a 'ScriptPlayer' component atteched to it, otherwise you will get a null reference. For completeness, you should be testing for this in your code by assigning the 'ScriptPlayer' component to a variable and testing for null. If it is null, then return from the function because there is nothing to do, or throw an error message because it should not be null.

3) The 'other' object in the onTriggerEvent is a Collider. The 'other' Collider is only a component of the 'other' GameObject being collided with. Even if your 'ScriptPlayer' component is working, you won't find it attached to the Collider component. Instead you need to reference the gameObject that the collider is attached to. Try the following (after renaming your script):

 ScriptPlayer scriptPlayer = other.gameObject.GetComponent("ScriptPlayer");
 if (scriptPlayer == null) return; // bail out of the function if no component is found
 scriptPlayer.lives -= 1;
Comment
Add comment · Show 4 · 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 XxCelosiaxX · Feb 17, 2014 at 04:25 PM 0
Share

Ok so I rename my Script to "ScriptPlayer" nd then insert the following code is this correct? Becasue now I get error code on line 28 saying ';' expected. Insert a semicolon at the end, but there is a semicolon there.

 function OnTriggerEnter (other : Collider)
 {
     if(other.gameObject.tag == "Player") 
     {
      
      ScriptPlayer scriptPlayer = other.gameObject.GetComponent("ScriptPlayer");
      if (scriptPlayer == null) return; // bail out of the function if no component is found
 
      scriptPlayer.lives -= 1;
 
       
       Instantiate(explosion,transform.position,transform.rotation);
     
       // Reset enermy position
       other.transform.position.y = 10.0;
       other.transform.position.x = Random.Range(-10,10);
     }
 }

This is what I add to my PlayerScript. I just add a new Var for life

 var lives                     : int = 3;                      // Player life
avatar image DragonMind · Feb 17, 2014 at 04:40 PM 0
Share

Why not: if(scriptPlayer != null) {scriptPlayer.lives -= 1;} ?

avatar image FuzzyLogic · Feb 18, 2014 at 01:39 AM 0
Share

@Dragon$$anonymous$$ind: That could work but assumes that it is safe to continue running the function even if lives is not decremented. $$anonymous$$ost times, you want to abort the function if something fails.

To be most correct, the code should throw a runtime error if scriptPlayer is null. It should not bypass the rest of the function and it should not skip decrementing lives. It should fail fatally, causing the app to crash, because this is a logic error and should be caught at development time.

@XxCelosiaxX: I don't use UnityScript so this is just a guess (C# is much better), but it looks like it is expecting a value to be returned at line 26. Replace this line:

 if (scriptPlayer == null) return; // bail out of the function if no component is found

with this line:

 if (scriptPlayer == null) return false; // bail out of the function if no component is found

If false doesn't work, then try null. If that doesn't work then I just don't know UnityScript well enough and someone else will need to chime in.

avatar image XxCelosiaxX · Feb 18, 2014 at 02:29 AM 0
Share

after trying that it said semicolon missing on line 28 but it does have ; there

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

22 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 avatar image avatar image

Related Questions

move object help [beginner] 1 Answer

NullReferenceExeption 1 Answer

Unity blue screen help? (Can't add scenes) [fixed] 2 Answers

How do i load my next level? 1 Answer

Script to teleport player (Beginner here) 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