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 zero3growlithe · Dec 28, 2011 at 04:44 PM · variablepublic

How to make variable public "In whole script" from update function?

If I want to make my:

"Input.GetButtonDown("Jump");" (Checked in Update function)

and

"PlayerControl = GameObject.FindWithTag("Player").GetComponent(FullShipController); (checked in Start function)

available from within whole script, what should I do?

(Example: i have Update function which checks for input and MoveShip function which... moves ship and PlaySound which plays sound when moving ship and i don't want to write same variable is different places)

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
3
Best Answer

Answer by spacepilot · Dec 28, 2011 at 05:01 PM

Declare the variables as private, at the beginning of your script, outside of any function. When assigning values to them inside a function, these values should get stored outside the functions but inside the script ( = "script-public").

Like this:

 private var PlayerControl : [sometype];
 private var Button : boolean; 
 
 function Start(){
  PlayerControl = [whatever];
 }
 
 function Update(){
  button = Input.GetButtonDown("Jump");
  blablabla...divide shoesize by age, multiply with euler-quaternions...blablabla
 }
Comment
Add comment · Show 3 · 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 zero3growlithe · Dec 28, 2011 at 05:29 PM 0
Share

Ah, never thought about that ^^ thx :)

avatar image spacepilot · Dec 28, 2011 at 10:24 PM 1
Share

You pushed my karma! Just got over 50 points now. Only 24 points left to reach next level. Tell me when you have another question :-D

avatar image Lo0NuhtiK · Dec 28, 2011 at 10:51 PM 0
Share

Null Reference Exception at line 5 on unity/answers.pre.code ; Object not set to an instance that allows splicing and stuff, "whatever" unrecognized, shoesize doesn't fit

avatar image
2

Answer by gegc · Dec 28, 2011 at 05:04 PM

The easiest way would be to declare a variable in the scope of the whole script and then update it when required, like so:

 bool doJump;

 FullShipController playerControl;

 void Start() {

     playerControl = GameObject.FindWithTag("Player").
                       GetComponent(FullShipController); 
     //Note: this will be null if the object is not player controlled.
     
 }

 void Update {
     if(Input.GetButtonDown("Jump")) {
         doJump=true;
     } else {
         doJump=false; //or set this to false when you actually do the jump
     }
 }
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

7 People are following this question.

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

Related Questions

Can I make variables visible to other scripts without making them visible in the Inspector? 1 Answer

Do something when something happens in another script. 3 Answers

How to delete a Variable in a script in game 1 Answer

How to deactivate something when holding down a key? 1 Answer

When to use private var. and public var.? 4 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