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 Akinup · Jul 28, 2013 at 03:03 PM · variablemultiple

Using a Variable over two Scripts

I have 2 scripts... one for the player and one for the enemy...

i want now a vector3 variable in the enemy script, that i can also use in my player script...

how can i do that?

if the variable changed in the enemy script, it must also changed in the player script....

i hope someone can understand me :/

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

4 Replies

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

Answer by Linus · Jul 28, 2013 at 03:27 PM

playerScript.js

     var enemy : enemyScript; //Type is the name of the script
     
     
     function Start(){
      enemy = GameObject.Find('enemy').GetComponent(enemyScript); //The game object where enemyScript is attached is named enemy
 //All public variables and functions on that script is now available from the enemy variable.
     enemy.someVector = Vector3(10,10,10);
     }


enemyScript.js

 //The variable needs to be declared inside this script
 var someVector : Vector3;

This will set the var someVector to 10,10,10 on start.

Do read the manual http://docs.unity3d.com/Documentation/Manual/Scripting42.html

Comment
Add comment · Show 6 · 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 Akinup · Jul 28, 2013 at 03:34 PM 0
Share

thank you very very very much! :DDD

avatar image Akinup · Jul 28, 2013 at 04:06 PM 0
Share

is there a way to get the script without finding a gameobject?

avatar image Linus · Jul 31, 2013 at 05:23 PM 0
Share

Sure there are, how do you want to get it? If your reason is that you have read that .Find is slow, its not a problem at all when only done once it Start()

avatar image Akinup · Jul 31, 2013 at 10:16 PM 0
Share

the problem is that the script on every enemy is... and i can only find the script of one enemy...

i want the script of the enemy i tag with the mouse for example... through a ray cast and a hit collider or with mouseover... ??

avatar image Linus · Aug 18, 2013 at 08:42 PM 0
Share

Web Search: GetComponent on clicked object Here is one previous answer on this http://answers.unity3d.com/questions/228974/enemy-select.html

Show more comments
avatar image
0

Answer by sfc.itzhak · Jul 28, 2013 at 03:19 PM

you can either use 1 static variable: http://answers.unity3d.com/questions/321385/static-variables.html

or use public variables and create a function that will upadate all other scripts //not recomnded.

or create 1 manger that holds the varibale and all scripts take the values from it (almost like a static var)

sfc

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 Akinup · Jul 28, 2013 at 03:48 PM

hmm... i do not really understand how static helps me :/

i have the variables:

public Vector3 enemy; public Vector3 reset;

in the Script "Enemy"

but even with Enemy.enemy or Enemy.reset, the Script "Player" doesn't find it...

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 Linus · Jul 31, 2013 at 05:24 PM 0
Share

please consider to convert this to a comment. Its how Unity Answers rolls.

avatar image
0

Answer by Rs31412 · Feb 18, 2014 at 06:57 AM

For those doing C# I found this following code to be the way to work.

 public int myVariable;
 
 
 GameObject myObject = GameObject.Find("objectInHeirarchy");
 myVariable = myObject.GetComponent<myObjectScript>().myVariable;
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

18 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

Related Questions

Accessing an Array in another function 1 Answer

foreach script check if boolean is true 3 Answers

I'm having more problems accessing a variable from another script in c# 2 Answers

How can I use a master script to send values to multiples objects 1 Answer

Share variables among scripts of the same object 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