Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 anhal · Sep 16, 2016 at 11:43 AM · c#static variable

Accessing Static variable from another class gives null value!

Hi all,

I am stuck with this error for many hours ... Will appreciate your inputs on this.

I am trying to use a variable as a global by declaring it static in class "Init". Initially, I am returning the Gameobject using FindWithTag into a static Gameobject variable in class "Init" and using its SetActive property to set it to false. Later on, I am trying to access this variable from another class "csblock1" and trying to reset it to true using "Init.hidez[0].setActive(true)". At this point, I am getting a console message "NullReferenceException: Object Reference not set to an instance of an object"

Even though I have initialized the static variable in class "Init", how does it return null when I am trying to access from class "csblock1" ?

Thanks in advance, Anhallink text

init.txt (1.5 kB)
csblock1.txt (597 B)
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 tanoshimi · Sep 16, 2016 at 11:57 AM 0
Share

Let's start with the obvious - have you definitely got a gameobject active in the scene hierarchy tagged with "zero1" at the start of the game?

avatar image anhal · Sep 17, 2016 at 01:44 AM 0
Share

@tanoshimi Yes I have. Actually, I am developing an x zero game for learning unity. ins$$anonymous$$d of x, i have cubes and ins$$anonymous$$d of zero i have spheres. The scene has these gameobjects and i had even succeeded in hiding these objects initially when the game starts. During this time, only black blocks are visible which are again cubes but not taking part in the game. Next, when I click on the block, depending upon the toggle flag, either a green cube or a pink sphere appears denoting either a x or zero. The init.cs is the initialization script and added as component to only one of the black blocks while the csblock1.cs is added to block 1, csblock2.cs to block 2 and so on upto csblock9.cs

avatar image anhal · Sep 19, 2016 at 11:16 AM 0
Share

Hi, I have one more observation. I have entered Debug statements in the Start() function. It appears the Start() function is called thrice. On first attempt, it stores the gameobject and on 2nd and 3rd attempts, the gameobject disappears. I have attached the updated Init.cs filelink text

init.txt (11.8 kB)

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by rmassanet · Sep 19, 2016 at 12:40 PM

As you say in your comments, your Start function might be called more than once. This is because your script is a MonoBehaviour and, as such, it can be attached to many GameObjects. However, they all share the static variable hidez.

In addition, keep in mind that FindWithTag will not find deactivated objects.

So, what is probably going here is this:

  1. First instance of Init correctly finds the desired GameObjects and deativates them.

  2. Second instance of Init does not find any object, so it overwrites the static variable with null objects.

  3. When csblock1 tries to access the static array, it finds only null values.

So, the solution: Make sure that there is only one instance of Init in the scene. Or even better, don't make this script a MonoBehaviour at all, unless you really need it. Make it a singleton.

Comment
Add comment · 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

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Renderer on object disabled after level reload 1 Answer

If I declare and initialize a public static variable in a prefab, will that variable retain its value or reset every time the prefab is instantiated? 1 Answer

Game freezes after restarting. 2 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