- Home /
Error with Update and OntrrigerEnter funtion
dear all, i tried to solve this problem alone. However this problem to tough for newbie like me. logically, i want to know how much speed that player have when enter a way (just say it is cube) this is the cube code
var data : manualscar;
data = gameObject.GetComponent("manualscar");
function OnTriggerEnter(other : Collider){
if(other.tag == "Player"){
if(data.currentSpeed < 5){
print("5");
}
}
}
and this is a player code
//to move the manual's car.
var maxSpeed = 0.0;
var minSpeed = 0.0;
var maxAcceleration = 0.0; // Controls the acceleration
var currentSpeed = 0.0;
var gear = 0;
var trackSpeed = 0;
function Update()
{
//to change the max speed, min speed, and acceleration
if(gear == 0){
currentSpeed = 0;
maxAcceleration = 0.0;
minSpeed = 0;
maxSpeed = 0;
}
else if(gear == 1){
maxAcceleration = 10.0;
minSpeed = 0;
maxSpeed = 20;
}
else if(gear == 2){
maxAcceleration = 8.0;
minSpeed = 11;
maxSpeed = 50;
}
else if(gear == 3){
maxAcceleration = 7.5;
minSpeed = 31;
maxSpeed = 80;
}
else if(gear == 4){
maxAcceleration = 6.0;
minSpeed = 51;
maxSpeed = 120;
}
else if(gear == 5){
maxAcceleration = 5.0;
minSpeed = 81;
maxSpeed = 180;
}
else{
maxAcceleration = 0.0;
minSpeed = 0;
maxSpeed = 0;
}
//to change the gear up with "1" and down with "2"
if(Input.GetKeyDown("1")){
if(gear != 5){
gear++;
}
}
if(Input.GetKeyDown("2")){
if(gear != 0){
gear--;
}
}
transform.Translate(Vector3.forward * Time.deltaTime * currentSpeed);
if(Input.GetKey("w")){
if(currentSpeed > maxSpeed){
currentSpeed -= maxAcceleration * Time.deltaTime;
if(gear == 4){
trackSpeed=currentSpeed+2;
}
else if(gear == 3){
trackSpeed=currentSpeed+1.5;
}
else if(gear == 2){
trackSpeed=currentSpeed+1;
}
else if(gear == 1){
trackSpeed=currentSpeed+0.5;
}
else{
trackSpeed=0;
}
currentSpeed = Mathf.Clamp(currentSpeed, minSpeed, trackSpeed);
}
else if(currentSpeed < minSpeed){
currentSpeed += 1 * Time.deltaTime;
trackSpeed = 0;
currentSpeed = Mathf.Clamp(currentSpeed, 0, maxSpeed);
}
else{
currentSpeed += maxAcceleration * Time.deltaTime;
currentSpeed = Mathf.Clamp(currentSpeed, minSpeed, maxSpeed);
}
}
if(Input.GetKey("s")){
if(currentSpeed <= minSpeed){
}
if(gear > 0 ){
currentSpeed -= maxAcceleration * Time.deltaTime;
currentSpeed = Mathf.Clamp(currentSpeed, minSpeed, maxSpeed);
}
}
if(currentSpeed > maxSpeed ){
}
if(gear == 0){
if(Input.GetKey("r")){
currentSpeed -= 200 * Time.deltaTime;
currentSpeed = Mathf.Clamp(currentSpeed,-10,20);
transform.Translate(Vector3.forward * Time.deltaTime * currentSpeed);
}
}
currentSpeed -= 2 * Time.deltaTime;
currentSpeed = Mathf.Clamp(currentSpeed, minSpeed, maxSpeed);
}
when i started the program there is no error, but when i hit the cube there is error like :
NullReferenceException: Object reference not set to an instance of an object
trackSpeed.OnTriggerEnter (UnityEngine.Collider other) (at Assets/script/trackSpeed.js:39)
hopefully someone know what i need. thx you very much.
but if i put funtion Update(){ code here } the program not work at all.
so the error is falling at
if(data.currentSpeed < 5){
? If so, then this part is not loading the script :
data = gameObject.GetComponent("manualscar");
either the script is not called manualscar, or it is not attached to the gameObject, therefore it is not found ( NullReferenceException: Object reference not set to an instance of an object
)
how to call it? i already read other problem and their solve.. how about $$anonymous$$e? i already set it to person.
Answer by Seth-Bergman · Jul 27, 2012 at 07:35 AM
var data : manualscar;
function Start(){
data = gameObject.GetComponent(manualscar);
}
no quotes, this way it returns the type of manualscar, otherwise it returns an Object, which would need to be typecast
EDIT: put it in the start function, and it'll work
second EDIT: you still need to DECLARE the var outside though, as above
next EDIT: if you need to find the script on another object, do this:
var data : manualscar;
function Start(){
var player = GameObject.FindWithTag("Player");
data = player.GetComponent(manualscar);
}
make sure the tag matches, and this should do it;)
It needs to be inside a function .. see my edit above :)
function Start(){ var data : manualscar; data = gameObject.GetComponent(manualscar); }
function OnTriggerEnter(other : Collider){ if(other.tag == "Player"){ print(data.currentSpeed); } }
i put like that and diffrent error. Assets/script/trackSpeed.js(38,23): BCE0005: $$anonymous$$ identifier: 'data'.
that's on line number 38 of a script called "trackSpeed".. check the script trackSpeed and see what is on line 38, you are referring to a var which doesn't exist in that script(or it isn't within scope, like if you declared it inside a different function)
trackspeed .js is the name of the file. that is the print data.currentSpeed itselft
Your answer
Follow this Question
Related Questions
Null Reference Exception Error. Modified Stats. Need Help. 1 Answer
NullReferenceException problem 2 Answers
NullReferenceException 1 Answer
3d Platform Tutorial Help 1 Answer
Error putting an object into an Array 0 Answers