nullrefrenceexception: how can i solve this?
so, ive been looking for 3 hours now, and i cant find why this is giving me an error, what im trying to do, is to lock the player (maxsidewaysspeed = 0.0f, jumping = false) and set the gravity, to get a thwomp powerup effect, so what im doing is: detecting if the player has obtained the speed powerup (you see it trying to check if the player has a maxSidewaysSpeed of 9.0f, wich is the value that the boost gives) then im checking if the player attacks, then i set all the variables. after that in the else, im setting the values back with the speedBoost value from earlier. im trying to access the standard script CaracterMotor. they arent on the same gameObject, nor are they in the same folder. here is my code:
#pragma strict
static var attack : boolean;
static var speedBoost : boolean;
function Start () {
}
function Update () {
if(script.movement.maxSidewaysSpeed == 9.0f){
speedBoost = true;
}
else{
speedBoost = false;
}
var script : CharacterMotor = GetComponent(CharacterMotor);
var AT = gameObject.GetComponent(AnimateTexture);
if(Input.GetMouseButton(0) && attack){
AT.rowNumber = 4;
script.movement.maxSidewaysSpeed = 0.0f;
script.movement.gravity = 70.0f;
script.jumping.jumping = false;
}
else if(Input.GetKey("a")){
AT.rowNumber = 3;
} else if(Input.GetKey("d")){
AT.rowNumber = 1;
} else if(Input.GetKey("left")){
AT.rowNumber = 3;
} else if(Input.GetKey("right")){
AT.rowNumber = 1;
} else {
AT.rowNumber = 0;
if(speedBoost){
script.movement.maxSidewaysSpeed = 9.0f;
script.movement.gravity = 20.0f;
script.jumping.jumping = true;
}
else{
script.movement.maxSidewaysSpeed = 6.0f;
script.movement.gravity = 20.0f;
script.jumping.jumping = true;
}
}
}
please help me, im stuck!
thanks in advance!
Answer by Oribow · Oct 04, 2015 at 05:42 PM
If your CharacterMotor isnt on the same GameObject, why are you trying to find it on the same GameObject? Look at this line:
var script : CharacterMotor = GetComponent(CharacterMotor);
"GetComponent" searches only on the GameObject the script is attached to! Try to make it a public var and set it in the Inspector.
public var script : CharacterMotor;
I can't figure it out, can you give me the debugged code please?
Whats the problem? Just exchange your line quoted in my post, with the second line quoted in my post and set the var "script" in the inspector.
Ok try this:
#pragma strict
static var attack : boolean;
static var speedBoost : boolean;
public var script : Character$$anonymous$$otor;
function Start () {
}
function Update () {
if(script.movement.maxSidewaysSpeed == 9.0f){
speedBoost = true;
}
else{
speedBoost = false;
}
var AT = gameObject.GetComponent(AnimateTexture);
if(Input.Get$$anonymous$$ouseButton(0) && attack){
AT.rowNumber = 4;
script.movement.maxSidewaysSpeed = 0.0f;
script.movement.gravity = 70.0f;
script.jumping.jumping = false;
}
else if(Input.Get$$anonymous$$ey("a")){
AT.rowNumber = 3;
} else if(Input.Get$$anonymous$$ey("d")){
AT.rowNumber = 1;
} else if(Input.Get$$anonymous$$ey("left")){
AT.rowNumber = 3;
} else if(Input.Get$$anonymous$$ey("right")){
AT.rowNumber = 1;
} else {
AT.rowNumber = 0;
if(speedBoost){
script.movement.maxSidewaysSpeed = 9.0f;
script.movement.gravity = 20.0f;
script.jumping.jumping = true;
}
else{
script.movement.maxSidewaysSpeed = 6.0f;
script.movement.gravity = 20.0f;
script.jumping.jumping = true;
}
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220612040018im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Help with variables 1 Answer
Show in text certain variables 0 Answers
Error BCE0051 with script JS 1 Answer
How to Modify a java Script variable with a c# Script 1 Answer