• 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 /
This question was closed Oct 16, 2014 at 09:01 PM by Landern for the following reason:

The question is answered.

avatar image
0
Question by TemplateR · Oct 16, 2014 at 07:53 PM · variablesmenustaticglobal

Changing static variables from another script?

Hello,

how can I change a static variable from another script? I would like to have an Option-Menu, where you can change like the amount of an ammo (for a little shooting Game).

Here is my script (called global) for the static variable of the ammo:

 #pragma strict
 static var ammo : int = 5;
 static var obstacleCount : int;

Does anyone have a solution?

Comment
Add comment · Show 7
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 TemplateR · Oct 16, 2014 at 08:09 PM 0
Share

How it will look like, when I use an input field or something else?

avatar image Landern · Oct 16, 2014 at 08:16 PM 0
Share

@TemplateR,

         global.ammo = GUI.TextField (Rect (10, 10, 200, 20), global.ammo, 25);
 
avatar image TemplateR · Oct 16, 2014 at 08:37 PM 0
Share

Here is my Option-Script, which I have an Error:

  global.ammo = GUI.TextField (Rect (10, 10, 200, 20), global.ammo,10);
  
  if (GUI.Button(Rect(10,10,200,20),"Spiel starten"))
  Application.LoadLevel("$$anonymous$$iShooter");



The Error is:

No appropriate version of 'UnityEngine.GUI.TextField' for the argument list '(UnityEngine.Rect, int, int)' was found.

avatar image Landern · Oct 16, 2014 at 08:48 PM 0
Share

you need to convert your ammo, of type int, to a string, TextField arguments are (UnityEngine.Rect, string, int).

So change your code to:

 global.ammo = Convert.ToInt32(GUI.TextField (Rect (10, 10, 200, 20), global.ammo.ToString(),10));
 

I'll be honest with you though, if text, alpha characters, non-numeric characters are entered, it will throw an exception. You may want to use a temporary variable that holds the conversion. If you don't need to update the variable in the textfield and it's for display, use a label, which is read only. You will still need to .ToString() the ammo because the expectation is a string not an int.

avatar image TemplateR · Oct 16, 2014 at 08:52 PM 0
Share

I have an another error:

$$anonymous$$ identifier: 'Convert'.

Show more comments

0 Replies

  • Sort: 

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

2 People are following this question.

avatar image avatar image

Related Questions

My static variable is not changing 2 Answers

Unable to modify a variable in another script 2 Answers

Problems with variables across scripts 0 Answers

Global Variables Refuse to Cooperate 1 Answer

Global Varible Problem 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