Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
2
Question by upariver · Jun 11, 2016 at 09:52 AM · c#scripting problemvariableset

Set a variable from another script

I know this question has been asked to death, however I see so many different answers and I haven't been able to make any of them work for me. Which way is the best one?

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 NoseKills · Jun 11, 2016 at 10:18 AM 1
Share

I haven't been albe to make any of them work for me. Which way is the best one?

:) what didn't work? What was the problem. I don't see a point in explaining the best way if you can't make it work anyways.

There is basically only one way to set a variable on another script. Get a reference to the script (instance) that has the variable and set it using that reference.

Yeah there's a few ways of doing this and the best way is the one you understand and the one that works.

1 Reply

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

Answer by Mmmpies · Jun 11, 2016 at 10:20 AM

Best or easiest?

The easiest way is to reference the name of the other script in a public variable and have the variable that you want to set public as well. Let's say you have a ChangeMe.cs script and an ApplyChange.cs script (the names aren't important I'm just using them for ease of explanation).

In ChangeMe you have a bool.

 public bool AmIChanged = false;

In ApplyChange you reference the script ChangeMe in a public variable.

 public ChangeMe myChangeMeScript;
 
 void Start()
 {
     myChangeMeScript.AmIChanged = true;
 }

Drag both scripts onto a GameObject (just a cube it's not important but they need to be in scene). Drag the cube onto the public slot for the myChangeMeScript script.

Look at the value of the bool and it should be false. Click play and it should turn to true.

You can also use getters and setters for private variables but get used to how you reference a script from another first then try one of the many tutorials that mention these.

Comment
Add comment · Show 5 · 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 upariver · Jun 11, 2016 at 02:21 PM 0
Share

Do they need to be in the same GameObject? What is they are in different GameObjects? I tried this but didn't work. http://pastebin.com/WXqqBGqv

avatar image Mmmpies upariver · Jun 11, 2016 at 02:37 PM 1
Share

No that public variable is the key, whatever object the script is on that shows that public slot in the inspector just needs whatever object the other script is on dragging onto that slot.

Is your script trying to interact with a Button on a Canvas?

If so then you also need

 using UnityEngine.UI;

at the top of the script.

avatar image upariver Mmmpies · Jun 11, 2016 at 03:17 PM 0
Share

Thanks, finally it works. So, if this is the easiest, what is the best one then, if you don't $$anonymous$$d answering?

Show more comments

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

171 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 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 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 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 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 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 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 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 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to access non static C# variable from another C# script 1 Answer

C# - Cannnot access variable in another script unless I get the component everytime. 1 Answer

Custom inspector. Pick one bool from a list. 1 Answer

Interchangeable animations in a script? 0 Answers

Change in Editor via Script Values of another Script 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