- Home /
Reference Not Set toInstance of Object
So I have a JS making a call to a CS script using GetComponent.
JS:
public static var upbutton : UpButton;
function Update ()
{
upbutton = GetComponent(UpButton);
upbutton.WasUpPressed(); //error here
Then there's the CS
CS:
public class UpButton : MonoBehaviour
{
public static bool upPressed = false;
void OnMouseDown()
{
upPressed = true;
}
public bool WasUpPressed()
{
return upPressed;
}
}
Problem is in the JS error line "upbutton.WasUpPressed();" the error says
"Object reference not set to an instance of an object"
Answer by GuyTidhar · Jun 27, 2011 at 08:02 PM
In this case, it means:
GetComponent(UpButton);
returned null, thus upbutton is null and thus you can not call the WasUpPressed function.
Do you actually have the component "UpButton" attached to the same game object you have the JS on?
Also, when you do this:
public static var upbutton = false;
You probably make upbutton a boolean variable. You must have meant doing this:
public static var upbutton;
Since you do not know what the variable is going to be, as you want to use duck typing.
But you should prefer doing this:
public static var upbutton : UpButton;
letting the compiler know before hand what you meant your variable to be and avoiding surprises during run time.
I actually did have: "public static var upbutton : UpButton;" $$anonymous$$is-write in code above, sorry (edited and fixed now). Yes I have the UpButton attached to the same gameObject. It is on a different hierarchy level of the gameObject though, not sure if that matters.
Different hierarchy ? $$anonymous$$aybe you mean you have it on a child game object?
It is on a child. I have a gameObject "$$anonymous$$enu". This has animations on it. Its child is "$$anonymous$$enuItems". This tells its parent to do the animations. It only tells it though if its children, UpButton or DownButton tell it they have been pushed.
To be clear. The JS and CS are not both viewable in the same inspector, though they are both attached to the same gameObject. Is it only possible to communicate between codes if they are both in the same inspector? Seems unlikely to me.
I even changed the function WasUpPressed to ins$$anonymous$$d return true ins$$anonymous$$d of the boolean value. Same error.
A child is not the same GameObject. The inspector is just a window - it shows the data attached to one game object at a time. Each game object has its own name, so if you place a new game object under an existing one in the hierarchy, it means you have a parent GameObject and a child GameObject. You can use this to find components in child game objects:
GetComponentInChildren(UpButton)
Your answer
Follow this Question
Related Questions
Simple question about OnMouseButtonAsUp 1 Answer
Multi similar script Components same game object 1 Answer
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
Can I access a script referenced in another script (without getcomponent)? 5 Answers
Using GetComponent in multiple scripts for same component? C# 2 Answers