• 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 user-860 (yahoo) · Jan 26, 2010 at 03:33 AM · gameobjectvariable

passing variable values

Hey I need to pass the value of a variable in one script to another script. Can I make an object of one script and use that to pass the value of the variable, or do I have to do it some other way?

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 Eric5h5 · Jan 26, 2010 at 03:40 AM

See the docs about accessing other objects.

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 hillargo · Jul 05, 2014 at 07:35 PM 0
Share

Just a heads up that this link no longer works.... I believe the new link is here

avatar image
1

Answer by thom · Jan 26, 2010 at 03:43 AM

There are a quite a few ways to do it.

The easiest, and least elegant way (certainly someone may bash me for this), is to make the variable static (C#) and access them by just ScriptName.VariableName. You can access functions or methods like that as well. Good and easy for central code blocks where you have one and only one script for the game

If you do a raycast or something and hit an object and you want to get the object's script you can always get it using GetComponent and cast it to your class:

MyScript t =(MyScript)touchStartedOnObject.GetComponent("MyScript");

Then you can get direct access to the public properties and methods.

Comment
Add comment · Show 2 · 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 duck ♦♦ · Jan 26, 2010 at 10:02 AM 2
Share

Even though you already said it's the "least elegant" way, I think it's worth pointing out that it's a common mistake that new programmers make, so it's best to not even suggest it as appropriate. It generally leads to confusing code structures and will limit your understanding of how to develop complex and solid game systems until you learn better methods. Static variables have legitimate uses, but there are far more appropriate methods shown in the documentation that Eric5h5 linked to in his answer.

avatar image Dejan1 · May 09, 2013 at 11:28 PM 0
Share

for c# variable has to be public static to acces it and change it directly in another script.

avatar image
0

Answer by Maltus · Mar 04, 2010 at 01:38 AM

I was thinking of having a GameMainObject that will hold alot of the detail of the game so when objects are instantiated, etc, they have same parent for various reasons


baseexample.

  • GameMainObject - js
    • HeroGun
      • Projectile-js
    • EnemyObject-js
      • WreckedObject
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 Honorsoft · Nov 28, 2016 at 01:01 PM

I miss the ease of use that Game Maker 4 had, it was a 2D (and basic 3D) game maker/compiler. It had a few nice features, such as declaring a variable as global (for example, global.myvar = 0; ) allowed it to be viewed/edited/accessed from ANY game object. Also, it had a simple way to access objects, just putting the object and the variable you want to access (eg, PlayerObject1.isAlive = 0). I am a C++ programmer who's a little rusty, I am trying to learn the bits I need like passing var's. I think I will try an old method from GameMaker4, making a persistant Controller object that stays in every game scene and has full control of variables.. until I learn a better way.

Comment
Add comment · Show 2 · 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 Mikael-H · Nov 28, 2016 at 03:11 PM 0
Share

You mean like (c#)

 public static float GlobalFloatVar;
avatar image Honorsoft Mikael-H · Jan 30, 2017 at 08:04 AM 0
Share

Yeah, I think so. I'm learning as I go, luckily C# is very similar to C++ so it's going well. There's a bunch of documentation on passing variables from object to object (class related I guess). Yeah, 'static' sounds familiar. A static variable can be easily be accessed? I'll look it up.

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.

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Get the Vector position of the player object when clicking another object C# 1 Answer

An Inheritance question. ( accessing non-static members from a script component) 2 Answers

Set Rigidbody variable to Prefab using code 1 Answer

Switching Gameobjects in a variable 1 Answer

How to create gameobject with different setting 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