Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by sharkspirit · Feb 10, 2013 at 10:56 PM · nullreferenceexceptiongetcomponentparent

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

Comment
Add comment · Show 3
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Wolfram · Feb 11, 2013 at 12:06 AM 0
Share

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?

avatar image sharkspirit · Feb 11, 2013 at 01:27 AM 0
Share

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

avatar image Wolfram · Feb 11, 2013 at 02:15 AM 0
Share

Thank you for answering all my questions. According to your answers, all seems in order, so unfortunately I'm out of ideas.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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).

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image sharkspirit · Feb 11, 2013 at 03:43 AM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

10 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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

NullReferenceException with GetComponent() C# 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges