Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 Grady · Jul 07, 2011 at 11:51 PM · gameobjectvariablesetdifferent

Set variable in script from a different script...

Hey guys,

I was wondering, how do i set the variable of a script from a different one....?

I know i have to use something like this:

 var objectWithOtherScript : GameObject;
 
 function Update(){
 
 objectWithOtherScript.GetComponent("scriptName")..........
 
 }

but i don't know what to do after that.....

Any help would be greatly appreciated!!!!

-Grady

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

3 Replies

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

Answer by Joshua · Jul 08, 2011 at 01:56 AM

Please use search in the future, this has been asked and answered many times. Second of all, you ussually want to use GetComponent.() instead of GetComponent( T );

Say you have script1 with only one line in it:

 var someVariable : int;

To change this variable from scrip2 you write, in the second script:

 objectWithOtherScript.GetComponent.< script1 >().someVariable = someNumber;
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 Grady · Jul 08, 2011 at 06:09 AM 0
Share

it says that it's unnaccessable because of it's protection level...

avatar image GuyTidhar · Jul 08, 2011 at 06:48 AM 1
Share

then you should make the variable public.

To access from different script:

public var someVariable : SomeType;

and Not: private var someVariable : SomeType;

avatar image Grady · Jul 08, 2011 at 07:21 AM 0
Share

thanks!!!!

avatar image Skibur · Jul 11, 2013 at 05:00 AM 0
Share

Is there a C# version of this code? I am getting some problems saying that it's cannot be accessed with an instance reference, qualify it with a type name ins$$anonymous$$d.

hit.transform.GetComponent< ScriptB >().setBool = true;

The bool is a public static variable.

avatar image GuyTidhar · Jul 11, 2013 at 07:32 AM 0
Share

is "ScriptB" a name of a type (class, variable, struct etc) or is it the name of the instance?

Cause it seems from the error you are getting you are using the name of the instance ins$$anonymous$$d of the name of the type.

Show more comments
avatar image
0

Answer by Hunden · Aug 16, 2014 at 09:29 PM

 void SpawnPlayer(){
         GameObject player = (GameObject) Network.Instantiate(playerPrefab, new Vector3(0f, 4f, 0f), Quaternion.identity, 0);
         //set the camera
         SmoothFollow script = (SmoothFollow) Camera.main.GetComponent("SmoothFollow");
         script.target = player.transform;
     }

Here's how i did it in C#.

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 Vinny-the-true · Oct 04, 2017 at 08:44 AM

OK, this is nice, but how can we do this from the inspector ?

What I ant to do is exactly what is done by the AnalyticsTracker with its parameters, so we may be able to select an object and then one of its property exactly the way you do it with UnityActions but with fields instead of methods !

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

9 People are following this question.

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

Related Questions

How do I change the text of a GUIText object through another GameObject using a variable? 2 Answers

how can I display a variable as a GUIText 5 Answers

How can I access a variable from another script in a separate object? 1 Answer

How to access gameObject variable script 2 Answers

Similar set of variables between classes... 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