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 1337GameDev · May 20, 2012 at 02:20 AM · instantiatereferencenull

C# class public variables null in all methods but start()

I have variables declared in my class defined outside of a method, but when i use them in any method other than start(), the object references are null. It is like all the object references get broken. Why would this be? I used an int to test, and the value is changed in my start function and i can use object references in start(), but in other functions they are all null for some reason.

My script is instantiated on a prefab, and then i reference the script on the prefab using this code:

     bObj = Instantiate(Resources.Load("BuilderPrefab") ) as GameObject;
     b = bObj.GetComponent("Builder") as Builder;

     b.setUpObj(positionOfQueue,new GameObject("dummyForBuilder"),false,this);
     b.startBuild(15f);
Comment
Add comment · Show 9
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 Berenger · May 20, 2012 at 03:09 AM 1
Share

the null reference should be a reference of dummyForBuilder's gameObject ? When does the exception occurs ?

avatar image 1337GameDev · May 20, 2012 at 03:28 AM 0
Share

sorry, the exception occurs inside the builder method setUp() and it complains about a script class variable i defined and used in start, but then it is "magically" null in other methods.

avatar image Kevin_C · May 20, 2012 at 03:46 AM 0
Share

do you think you could give a run-able example that demonstrates this behavior?

avatar image 1337GameDev · May 20, 2012 at 04:34 AM 0
Share

Here is my code for my builder class:

//beginning of class (extends monobehavior)

public GameObject debugObj; //game object with script to be found public DebugGuiText debugGText; //script

//inside start

debugObj = (GameObject)GameObject.Find("Debug-gText");

//this passes the check for not being null

debugGText = (DebugGuiText)debugObj.gameObject.GetComponent("DebugGuiText");

//is not null as well

debugGText.permDebug("done with start method");

//shows text to guiText object, actually shows up and works

//update()

debugGText.permDebug("inside update method call");

//public void setUp(float n)

debugGText.permDebug("inside setUp()");

The behavior is that update is skipped (never executes, even if i omit the line of debugGText and put a test int and incriment it. Instantiate the builder and start() runs and prints out the message to the guiText object. When i call the setUp method, the game complains about a null reference at that line where permDebug is called. If i put an if statement to check if null and skip, it sucessfull skips and avoids the null reference. If i tell it to check if obj is null then execute permDebug, it gives a null reference.

What could cause this?

avatar image Drakestar · May 20, 2012 at 05:49 AM 0
Share

Based on the code you posted, debugGText is scoped inside the Start() method (only debugObj is a field). Is that true? If so it will go out of scope as soon as you leave.

Show more comments

0 Replies

· Add your reply
  • Sort: 

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

7 People are following this question.

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

Related Questions

NullReferenceException on Photon Unity RPC Call 1 Answer

NullReferenceException Object reference error 1 Answer

I am getting the error of NullReferenceException? 1 Answer

Instantiate() as GameObject = null reference 1 Answer

NullReferenceException - With Network PlayerSpawn 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