- Home /
NullRef on my parent script
Ill try explaining this the best i can since english isnt my first language.
So im very new to C# and Unity... im trying to do a RTS, and for this question there are 3 important scripts.... Buildings, CivBuilding(has specific civilization stuff for the buildings) and then a specific script for the main building.
i want to have the building firstly without collisions and following my mouse and after i place it, only then i will enable the collision, since i want this behavior on all the buildings i did it on the "buildings" script and i tough since CivBuilding inherits its behavior and the main building script inherits CivBuilding behavior then it would work.
but i get NullReferenceException : Object reference not set to an instance on these lines
SphereCollider sphere;
public virtual void Start () {
Built=false;
sphere = gameObject.GetComponent<SphereCollider>();
here-> sphere.enabled=false;
}
i think its because both the gerenal building script and the civBuilding script arnt attached to any object...but since my building's class is abstract i cant add it to any object...
btw, the script works as intended because once it gets to my main building script it recognizes the collider...but i didnt really wanted to have errors on my game...should i use throw exceptions??
sry for the long text and thanks for any answers
Even after reading your text twice, the situation is still unclear. What is your inheritance structure? Which scripts are envolved? Which script is deriving from which other script*? And most importantly, which script does the code you posted belong to?
Also, since you placed "virtual" in front of Start(), I assume you are deriving from it somewhere. Do you call base.Start() in all derived classes? Otherwise your code fragment will not be executed at all.
You say "i get ... on these lines", plural, but you only show us one line. What are the other lines?
And sorry for asking the obvious, but - you did triple-check that all objects that have this script attached to actually have a SphereCollider? Do you attach the SphereCollider via script, or was it applied in the Editor?
buildings : monobehaviour
CivBuildings : buildings
mainBuilding : CivBuildings
the code i posted was in buildings
i did base.Start() on CivBuildings and mainBuilding
it did another Nullref some lines later on sphere.enabled=true; so mid writing i decided not to post that part of the code since is the same.
i applied the spherecollider via Editor and i still only have 1 object that has the mainBuilding script attached and yes i did triple checked
Thank you for answering all my questions. According to your answers, all seems in order, so unfortunately I'm out of ideas.
Answer by Wolfram · Feb 11, 2013 at 02:21 AM
The only ways the code you posted can result in a NullReferenceException in line 6 are the following:
that's not the whole Start() method and you removed some lines for readability
the object that has this script (or a derived class) attached does not have a SphereCollider attached
this script is accidentally attached to an additional object which has no SphereCollider
the SphereCollider gets destroyed by a script at some point
One of these must be true. Since your script is working as intended, maybe it's #3. Maybe a prefab prototype or something similar. Use "Find References in Scene" in the context menu in the "Project" tab on all building classes to find their scene instances (assuming you don't attach/create building objects at runtime).
its : the object that has this script (or a derived class) attached does not have a SphereCollider attached
because i the script that has that code is abstract ...i want the script behaviour to be inherited for all buildings, thats the reason it works...
the building script (has the code i posted) isnt attached to any object so it gives null
the civbuilding script (inherits from above) is attached to an object but doesnt have sphere collider...and i dont really want it to have...hes supposed to only have a dictionary of available buildings and specific behaviors
the mainbuilding script (inherits from civbuilding that inherits from building) IS attached to an object that has sphere collider and the script works as intended
but until it gets to the mainbuilding it gives null errors on the building script...i hope everybody understands now...its frustrating for me to be so bad at explaining and i bet for u guys that dont understand me aswell :(
Your answer
Follow this Question
Related Questions
How can I access another script in my prefab to set a variable after my network player setup runs? 1 Answer
Change SetActive(true/false) from a GameObject which is child of a different ImageTarget 1 Answer
Connecting Multiple Rigidbodies with Collision 0 Answers
I'm having more problems accessing a variable from another script in c# 2 Answers