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 riko4628 · Mar 24, 2013 at 08:57 AM · gameobjectvariableconvertcountfound

Using variables from other script and converting them to another variable

Hello,

In my game i have about 6 areas that must be discovered by player and take photo of them, if player did this, it will play a sound and it change variable found to 1(in that GO). So what i want to do i want to count how many areas he discovered by another script and convert it to another variable called foundcount, and then i will add some other things such as how many areas he discovered on gui and stuff :)

Comment
Add comment · Show 1
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 riko4628 · Mar 24, 2013 at 01:34 PM 0
Share

how can i use variable from other script?

2 Replies

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

Answer by CatDevPete · Mar 24, 2013 at 02:14 PM

If you want to access variables in other scripts, there are several ways to do this.


  • You can set the variable to be global, so all you need is the script and variable name.

eg. If the variable 'foundCount' is in script 'TestScript', you access it by using:

TestScript.foundCount

So, incrementing it would be:

TestScript.foundCount ++;

You can set variables to be global when declaring them by adding the 'static' keyword in front, like:

static var foundCount : int = 0;


  • If you don't want the variable to be global, you need to know which Game Object has the script with the variable. When you declare a gameobject variable,

var objectVariable : GameObject;

...You can either use the inspector to set the object with the script, or use:

objectVariable = Find("GameObject_Name");

(People will tell you that using Find() is very performance-heavy, though: http://docs.unity3d.com/Documentation/ScriptReference/GameObject.Find.html)

Once you got the game object, you use:

objectVariable.GetComponent(Script_Name).foundCount

...to access the variable.


  • Use functions to send values then assign them in the other script.

Using functions ensures good encapsulation practices, and allows you to change and edit the variable names and types without any issues. You will still need to target the Game Object first, then use:

objectVariable.functionName()

For functions, it's best if you read up more about it here: http://forum.unity3d.com/threads/34015-Newbie-guide-to-Unity-Javascript-(long)

Comment
Add comment · Show 1 · 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 riko4628 · Mar 24, 2013 at 04:22 PM 0
Share

Thank you very much :)

avatar image
1

Answer by stevemk14ebr · Mar 24, 2013 at 02:08 PM

Method 1)you need to first make a referance to your other script, note that the gameobject must have both scripts attached

 //replace vp_FPSController with your script name
     Controller = gameObject.GetComponent<vp_FPSController>();

then you can simply do

 Controller.found=1;

replace found with the public variable you are trying to change from the other script.

Method 2) if the script is not on the same gameobject you still need to referance it so make a variable with a referance then drag that game object onto the variable in the inspector

 //MUST CHANGE THE vp_FPSController to the name of the
 //script you are trying to get the variable from
 public vp_FPSController controller;

then you can change the variable in the same way

 //variable found must be public
 vp_FPSController.found=1;

3) if when you make that variable public you don't want it show in the inspector you can put

 [HideInInspector]

above it

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

13 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

Related Questions

Make a variable start at 10 and count down until 0 3 Answers

Store previous gameObject that raycast had hit. (Or store any previous value in Update for that matter) 1 Answer

Decrease variable after pressing a button 1 Answer

Changing a variable across multiple instances of a GameObject 1 Answer

How to save a gameObjects on a prefab and variables with a script? 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