- Home /
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?
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!
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.
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;
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
Why not: if(scriptPlayer != null) {scriptPlayer.lives -= 1;} ?
@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.
after trying that it said semicolon missing on line 28 but it does have ; there
Your answer
Follow this Question
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