- Home /
C# - instance is not set to an instance of an object
I am a noob when it comes to c#, but i am getting new errors everytime i post a question about the previous question, so i thought: maybe someone can clean this script up, i am using the caracter motor from the standard assets and i want to change the value of jumpHeight because one platform has a powerup but that platform is higher than the players origional jump, so the player needs a powerup with this script, but this script doesn't work, i think the problem lies in the 'caracterMotor motor;' line, so my question is: can someone PLEASE fix this? thank you!
using UnityEngine;
using System.Collections;
public class jumpUp : MonoBehaviour
{
private CharacterMotor motor;
public GameObject player;
public bool enable;
public bool destroy;
void start(){
motor = player.GetComponent<CharacterMotor>();
bool enable = true;
bool destroy = true;
}
void OnTriggerEnter(Collider other)
{
if(enable == true){
motor.jumping.baseHeight = 2.0f;
Destroy(gameObject);
}
else
{
motor.jumping.baseHeight = 1.0f;
Destroy(gameObject);
}
}
}
What does the NullReferenceException exactly say? I assume it happens because your motor variable is null and you never assign it. $$anonymous$$ake it public and assign it in the Inspector or get the reference in a Start() function. The two if statements at the start of your OnTriggerEnter
function don't make sense since you set the variable to true right before (on declaration). Also line 15 and the following line say if enable is true -> if enable is false then... which is not possible. The codeblock on line 32 is empty. I suggest to start with tutorials if you are new. Another tip is if you happen to have an error/not working script, go trough the script step by step and think about what you want it to happen and what actually happens. This helps to avoid errors in your logic.
it says: instance is not set to an isnstance of an object... i edited my post to my code now, however, still, when i collide with the powerup, it gives me that error
Answer by Ibzy · Jul 16, 2015 at 11:57 AM
You need to reference the relevant components. Currently you are creating null variables and not assigning them a value.
Create a void Start() function and, providing this script is attached to the player, use motor = GetComponent<CharacterMotor>();
If this is on the powerup, then you have to get the player-object's charactermotor component.
void OnTriggerEnter(Collider other) {
Character$$anonymous$$otor motor = other.GetComponent<Character$$anonymous$$otor>();
if(motor != null) {
//now you can access public members of the player's motor script
}
}
THAN$$anonymous$$ YOU SO FREA$$anonymous$$IN $$anonymous$$UCH THAN$$anonymous$$ YOU!!!!!!!!!!!
Answer by Hexer · Jul 16, 2015 at 12:57 PM
you dont need to reference or assign CharacterMotor in your script. From my point of view it is confusing. I would have make the jumping.baseHeight a static variable.
https://unity3d.com/learn/tutorials/modules/intermediate/scripting/statics
Edit: oops pasted wrong link(fixed).