Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 amelnik · Dec 30, 2013 at 07:12 PM · c#gameobjectawakestatic class

NullRefrenceExeption on Awake and in some other places

In a dart game I'm making in 3d I have a static class for which I go for references of objects in the game. Here it is:

 public class Reference : object 
 {
     public static ScoreTextScript scoreText = GameObject.FindGameObjectWithTag("ScoreText").GetComponent<ScoreTextScript>();
     public static ForceTextScript forceText = GameObject.FindGameObjectWithTag("ForceText").GetComponent<ForceTextScript>();
     public static Shoot shoot = GameObject.FindGameObjectWithTag("Player").GetComponent<Shoot>();
 
     public static GameObject[] GetDartsInScene()
     {
         return GameObject.FindGameObjectsWithTag("Projectile");
     }
 
     public static GameObject[] GetTargetsInScene()
     {
         return GameObject.FindGameObjectsWithTag("Targets");
     }
 }

I call some it's variables and methods almost in every script (later I'll give the places where I call it). When the game runs ~3 Errors like this are given immediatly (these are the full details):

NullReferenceException: Object reference not set to an instance of an object

Reference..cctor () Rethrow as TypeInitializationException: An exception was thrown by the type initializer for Reference Level_LimitedDarts.Awake () (at Assets/Scripts/GameController/Level Scripts/Level_LimitedDarts.cs:14) Afterwards more errors like this are given every frame. Here are the awake methods of the class and its base class: protected virtual void Awake() { Reference.scoreText.ScoreChanged += (int Score) => score = Score; score = 0; screenFade = GameObject.FindGameObjectWithTag("ScreenFader").GetComponent(); screenFade.FadedOut += () => WinState(winState);//Thus endeth thy scene sceneEnds = false; winState = false; } protected virtual void Awake() { Reference.scoreText.ScoreChanged += (int Score) => score = Score; score = 0; screenFade = GameObject.FindGameObjectWithTag("ScreenFader").GetComponent(); screenFade.FadedOut += () => WinState(winState);//Thus endeth thy scene sceneEnds = false; winState = false; } All of the events and variables mentioned in these methods exist. I've tried re-adding all the scripts mentioned. I've tried removing the 'protected' and/or the 'override'. Thanks in advance.
Comment
Add comment
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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by potatoking · Dec 30, 2013 at 09:34 PM

Maybe the gameobject you are trying to find with findgameobjectwithtag() is not instantiated yet, so you can't find anything.

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
avatar image
0

Answer by amelnik · Dec 31, 2013 at 05:46 PM

If so, how can I solve the problem? When I debugged the project the first time it said that the value of Refrence.shoot (for example) is reference.shoot or something like that. When I debug it the breakpoint in the Reference script is pink and a non-functioning. When I remove it the debugging still does nothing. sigh.

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

19 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How many times awake function is called when toggle on/off gameObjects with the same C# script 1 Answer

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Finding the Vector3 position of a game object 1 Answer

Dynamically Changing GameObject makes NullReferenceException(C# Unity) 1 Answer


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