- Home /
Object reference not set to an instance of an object
Hey there,
I have two C# scripts; one is a throttle lever (throttlelever.cs) that when clicked, should be triggering an iTween animation; the object to activate being given (caraccelerate.cs).
My error is
throttlehover.OnMouseDown () (at Assets/throttlehover.cs:17) UnityEngine.SendMouseEvents:DoSendMouseEvents() I'm Guessing they can't find one another, but I hope someone will be able to enlighten me on why that is. My throttle script: using UnityEngine; using System.Collections;NullReferenceException: Object reference not set to an instance of an object
public class throttlehover : MonoBehaviour { public caraccelerate accelerate; void OnMouseEnter () { renderer.material.color = Color.red; } void OnMouseExit () { renderer.material.color = Color.white; } void OnMouseDown () { accelerate.OnAccelerate(); } } My caraccelerate vehicle script: using UnityEngine; using System.Collections;
public class caraccelerate : MonoBehaviour {
public void OnAccelerate () { iTween.MoveTo(gameObject, iTween.Hash("path", iTweenPath.GetPath("centralline"), "time", 50, "easetype", iTween.EaseType.easeInOutSine)); }
} Kind regards,
It sounds like you didn't assign anything to your accelerate variable. In the inspector since your accelerate variable is public you can just drag and drop the caraccelerate gameobject into that variable.
Or in your start method in the throttle script you can put something like
accelerate = GameObject.FindObjectOfType(typeof(caraccelerate));
which will find a reference to a game object with a caraccelerate component on it and assign it to accelerate. (warning if there are more than one caraccelerate gameobjects it may not assign the correct one to your object)
So, basically your problem is your accelerate variable is empty/null and you need to assign it a caraccelerate reference.
I believe this needs to be added to AT$$anonymous$$Ethan's line of code:
accelerate = GameObject.FindObjectOfType(typeof(caraccelerate)) as caraccelerate;
Yes yes, use dannyskim's code snippet I forgot to add the "as caraccelrate" part. $$anonymous$$ine will generate an error complaining about casting a gameobject component to a caraccelerate component
@dannyskim and @AT$$anonymous$$Ethan: These should really be answers and not comments. Its hard to mark an answer as the correct answer when it is a comment.
You're right Starkid225, sorry about this inconvenience. I made my comment an answer now.
Answer by ATMEthan · Feb 18, 2013 at 07:21 PM
It sounds like you didn't assign anything to your accelerate variable. In the inspector since your accelerate variable is public you can just drag and drop the caraccelerate gameobject into that variable.
Or in your start method in the throttle script you can put something like
accelerate = GameObject.FindObjectOfType(typeof(caraccelerate)) as caraccelerate;
which will find a reference to a game object with a caraccelerate component on it and assign it to accelerate. (warning if there are more than one caraccelerate gameobjects it may not assign the correct one to your object)
So, basically your problem is your accelerate variable is empty/null and you need to assign it a caraccelerate reference.
Answer by Exagerate · Feb 18, 2013 at 07:22 PM
Thanks guys! i was able to actually drag the caraccelerate (vehicle) from the Hierachy onto the Throttle script in the inspector and it did pretty much the same thing.
Kind regards,
Hey, my answer my have not been posted before but they are there now. So, accept the answers as right so other users will know this helped/worked. :)
Your answer
Follow this Question
Related Questions
iTween NullReferenceException error 1 Answer
Using iTween with a prefab tree. 0 Answers
iTween call producing NullReferenceException 3 Answers
iTween - Null Reference 0 Answers
iTween nullref error. Hashtable syntax problem? Not sure. 3 Answers