"Object reference not set to an instance of the object" yet I can see the reference is working in monodevelop
As title says, using this code to access a function in another script
GameObject castAccess = GameObject.Find("castInt(clone)");
castInt castScript = castAccess.GetComponent("castInt") as castInt;
castScript.removeSelf();
it returns the error Object reference not set to an instance, I have no idea which reference is not set to an instance as I have been through everything...
monodevelop autocompletes the castScript.removeSelf(); call so it finds the removeSelf function in the other script on the other class...
the object "castInt(clone)"
is an instantiated prefab which has the script castInt attached to it, it's created on button pressDown and then the code here is called on buttonPressUp
on top of all this I have identical code seemingly working identically in another scene that doesn't error!
any insight would be fantastic!
Autocomplete has nothing to do with finding null reference exceptions. You have written classes with methods and variables in them and autocomplete knows which class has which methods and variables. A null reference happens when you run your code and try to reference a property of something that doesn't exist.
GameObject go = null;
Debug.Log(go.name);
You will get autocomplete on "go.name" because the class GameObject will always have that variable available. But this will throw a null ref when you run it because go is null and there's no gameObject that could give you its name.
To (over)simplify, any variable on the left side of a dot (.) is a good place to start looking. You have 2 of those
castAccess.GetComponent("castInt")
And
castScript.removeSelf();
The error you get should show you which line causes the error. Either castAccess is null because GameObject.Find can't find an object by that name or castScript is null because castAccess doesn't have that component.
When you find out which it is, it's easier to find out why. But we can't help u much more without knowing more about the situation
so while messing around trying to get the class to remove the gameObject I used a destoryImmediate and lost some stuff permanently, so unfortunately I'll be starting from scratch with this one, clean slate and all that.
In regard to your solution surely the auto-complete would only function if the reference was working as the thing it was auto-completing was in another class? if there was no reference then how did it no what to talk to? Or can unity access other classes properties without a reference but they won't function unless you do reference?
Thanks for taking it slow and giving me the explanation I am sure I will be referring back to this as I rebuild this script!
Answer by spooneystone · Nov 04, 2015 at 11:48 PM
Try the second part like this castInt castScript = castAccess.GetComponent();
castInt castScript = castAccess.GetComponent<castInt>();
its no problem :) Sorry for taking a while to reply but i'm still getting "object reference not set to and instance of an object" error when I try and call the function, the code itself is working out but some reference is still failing.
Your answer
Follow this Question
Related Questions
Why is the this object referenced to itself? 1 Answer
Instatiated object not being referenced in Start function? 2 Answers
Getting Int value from script to other script 2 Answers
The referenced script on this Behaviour is missing - on any new script 0 Answers
Referencing instances of scripts based on the GameObject they are attached to. 0 Answers