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 Dok101 · Apr 13, 2014 at 08:51 PM · c#guierrorgameobjectstatic

Displaying a static variable from another script with OnGUI

Hello I am trying to access a variable from another script and then display it on OnGUI. In one script called Career_Variables I have 6 variables : Fame, Fame_level, money, Races_Completed, Races_won, Races_lost. I have already been able to access the variables from Career_Variables in another script so I dont think that its a problem with gameobject.FindObject.GetComponent

Here's the code of the script with the GUI

 using UnityEngine;
 using System.Collections;
 
 public class Career_GUI : Career_Variables {
     
     Texture StartRacing;
     GUIStyle bigger_option_GUI_Style;
     GUIStyle smaller_option_GUI_Style;
     GUIStyle two_smaller_option_GUI_Style;
     
     
     void Start () {
 
     }
     
     void Update () {
         Career_Variables career = GameObject.Find("Background Camera").GetComponent<Career_Variables>();
 
     }
     
     void OnGUI()
     {
         GUI.Label(new Rect(605, 8, 20, 20),  money.ToString());
         GUI.Label(new Rect(850, 100, 20, 20),  Races_Completed.ToString(), smaller_option_GUI_Style);
         GUI.Label(new Rect(1155, 130, 20, 20),  Fame.ToString(), two_smaller_option_GUI_Style);
         GUI.Label(new Rect(1319, 100, 20, 20),  Races_won.ToString(), two_smaller_option_GUI_Style);
         if (GUI.Button(new Rect(5,140,265,180), StartRacing))
             Application.LoadLevel ("Level Select");
     }
 }

And this is the code of the class that holds the variables

 using UnityEngine;
 using System.Collections;
 
 public class Career_Variables : MonoBehaviour {
 
 static public int Fame;
 static public int Fame_level;
 static public int money = 20000;
 static public int Races_Completed;
 static public int Races_won;
 static public int Races_lost;
 
 void Start () {
 
 }
 
 void Update () {
 
 }
 
 
 }

I keep getting the errors

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

UnityEngine.GUI.Label (Rect position, UnityEngine.GUIContent content, UnityEngine.GUIStyle style) UnityEngine.GUI.Label (Rect position, System.String text, UnityEngine.GUIStyle style) Career_GUI.OnGUI () (at Assets/Scripts/CSharpScripts/Career_GUI.cs:27) and

UnityException: You are not allowed to call this function when declaring a variable.

Move it to the line after without a variable declaration. If you are using C# don't use this function in the constructor or field initializers, Instead move initialization to the Awake or Start function. Career_GUI..ctor () I've tried to decode these errors but i'm totally confused. All help will be appreciated Thank you in Advance
Comment
Add comment · Show 4
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 Dok101 · Apr 13, 2014 at 09:30 PM 0
Share

I put public before all the GUIStyle's and StartRacing texture's but it still shows the same errors

avatar image getyour411 · Apr 13, 2014 at 09:31 PM 1
Share

Putting public doesn't by itself fix this, once public you then have to drag/drop the proper elements into those in the Inspector fields

avatar image Dok101 · Apr 14, 2014 at 07:29 AM 0
Share

yeah they only came into inspector when I typed public. Thanks!

avatar image Dok101 · Apr 14, 2014 at 07:52 AM 0
Share

Thanks that solves my problem.

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by getyour411 · Apr 13, 2014 at 09:21 PM

Looks like you declared some private GUIstyle references and the StartRacing Texture but I don't see you setting anything before you use those.

Edit: glad you got it worked out, I changed comment to an Answer.

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

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

Related Questions

Adding score when enemy dies (Errors) 2 Answers

Why is networkbehaviour underlined in green? 1 Answer

3 Days and I still can't figure out c# reflection, please help? Trying to use a protected Type and method. 1 Answer

Multiple Cars not working 1 Answer

Static gameObject? 3 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