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 aagrlp640 · Oct 21, 2013 at 04:30 PM · accessing from any scriptanother script

Change a value of a script from other script

Hi, i have a question, how can i access to a property of a script and change it.

For example: alt text

I have a script called Enabled that disable Rotate Script, and it works, but how can i acces to Speed property and change it trought another script? lets supouse one called Speed that change value of speed Thanks

rotate.png (17.0 kB)
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 robertbu · Oct 21, 2013 at 04:53 PM 0
Share

Very common question. Google: "Change a value of a script from other script Unity3d"

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by mattmanj17 · Oct 21, 2013 at 06:40 PM

see Accessing variables in other script Components

on this page http://docs.unity3d.com/412/Documentation/Manual/Scripting.html

also, you could have found this by putting the title of your question into Google, like robertbu said.

Comment
Add comment · Show 3 · 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 michaelagill92 · Jan 12, 2021 at 03:28 AM 0
Share

Now your answer is a dead end, because the link you posted leads nowhere. So Im here in 2021 looking for an answer to this question and the trail is dead because you haven't answered it.

avatar image mattmanj17 michaelagill92 · Jan 12, 2021 at 03:40 AM 1
Share

wow, this is a blast from the past. It sucks that the link broke. This answer on another question seems to be helpful. http://answers.unity.com/answers/42845/view.html

in the interest of making this a bit more future proof, here is a snip of one of the more brief answers on that question.

if you have a script on an object named 'ThePlayer' that looks like this.

 public class PlayerScript: $$anonymous$$onoBehaviour 
 {
          public float Health = 100.0f;
 }

then you can fiddle with Health from another script like this

 public class Accessor : $$anonymous$$onoBehaviour 
 {
      void Start()
      {
           GameObject thePlayer = GameObject.Find("ThePlayer");
           PlayerScript playerScript = thePlayer.GetComponent<PlayerScript>();
           playerScript.Health -= 10.0f;
      }
  }

I wish you luck using unity in 2021. Sorry if me from 7 years ago made your day a little worse.

avatar image michaelagill92 · Jan 12, 2021 at 11:39 AM 0
Share

I owe an apology. I did not mean to come across as snappy, I had just gotten frustrated because I had found many answers like this that linked to a broken web page.

I would like to ask about this however.

 PlayerScript playerScript = thePlayer.GetComponent<PlayerScript>();

the above line will create an instance of the class 'PlayerScript' right? So It will essentially run the Start() method again and will not have the variable values that have been changed over time of the game running. Its not the same instance of the script I had when I first ran the game.

I wish to call a method that returns which wave number I am on. for relational sakes: PlayerScript playerScript = thePlayer.GetComponent().GetEnemyHealthBasedOnWaveNumber();

Any ideas? I could also be thinking of the totally wrong approach to the issue. Thank you.

p.s. also quite impressed you responded so quickly considering this was 7 years ago.

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

16 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

Related Questions

EnemyAi script 1 Answer

Trouble with calling a variable from another script 5 Answers

Getting Monobehavior script from inspector 0 Answers

triggering an event in another C# script 1 Answer

Access a Script from Another Scene? "Object Reference not set..." 0 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