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 /
This question was closed Dec 22, 2013 at 09:19 PM by Eric5h5 for the following reason:

Duplicate Question

avatar image
1
Question by infamouslyuseless · Dec 22, 2013 at 04:01 PM · javascriptvariablesyntax

Changing The Value Of A Variable From Another Script (JavaScript)

I have my script called PlayerAnimations, and a script called ColliderTrigger1. Player animations is like this (javascript):

 var parkour1 = false;
 
 function Update ()
 {
     if (parkour1 == true)
     {
          animation.Play("parkour1")
     }
 }

and I want the script ColliderTrigger1 to do something like this:

 //Get the parkour1 variable from script PlayerAnimations
 
 function Update ()
 {
     
     if (Input.GetKey(KeyCode.G))
     {
         //parkour1 = true;
     }
     
 }


How would i do this. I want to access the variable from the script, then change it to true if player hits 'G'. I just don't understand how to get the variable from the script, then how to change it to true.

Comment
Add comment · Show 4
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 Josh707 · Dec 22, 2013 at 09:06 PM 0
Share

Assu$$anonymous$$g your scripts are on separate, unrelated gameobjects, you can use a public & static variable to access it from another script without a direct reference (variable, getcomponent, etc) to that script instance. You can use it just like this (C#):

 //In PlayerAnimations
 public static bool parkour1 = false;

 //In 2nd script
 PlayerAnimations.parkour1 = some_value;

$$anonymous$$eep in $$anonymous$$d with a static variable the value belongs to the type itself, not an instance, so if you try to reuse this script on 2+ players at the same time, their static variables will share the same value, and will both be changed when you set it.

avatar image Eric5h5 · Dec 22, 2013 at 09:19 PM 0
Share

The question specified JS, not C#. Also you would not use static variables just to get variables on another script; please don't recommend that since it leads to complications if the person using it doesn't understand what static means.

@infamouslyuseless: this is a massively duplicated question. Every possible variation of this question has been asked and answered many times, and seriously does not need to be asked again since the answer is very easily found with a search. Thanks.

avatar image infamouslyuseless · Dec 22, 2013 at 09:55 PM 0
Share

i do understand what static means...

avatar image Josh707 · Dec 22, 2013 at 11:13 PM 0
Share

I'm not sure how to properly declare variables in JS but I'm sure he can figure it out - something like this:

 public static var parkour1 : bool = false;

As for the approach, I do agree, normally I wouldn't make a variable static just to get to it. I figured it's a pretty simple way to access it assu$$anonymous$$g he only has one instance of his player, but yeah a quick search on the forums will yield a bunch of proper answers for this.

1 Reply

  • Sort: 
avatar image
1

Answer by alexanderflink · Dec 22, 2013 at 04:27 PM

Hi! I am quite new to Unity myself, but I believe this is how you would achieve this. First, you must create a reference to the PlayerAnimations script from the ColliderTrigger1 script, like so:

 var playerAnimationsScript: PlayerAnimations;
 
 function Start () {
     playerAnimationsScript = GetComponent(PlayerAnimations);
     
 }

Now, you can manipulate the parkour1 variable like so:

 function Update () {
     if (playerAnimationsScript.parkour1 == "some value") {
          playerAnimationsScript.parkour1 = "some other value";
     }
 }
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 infamouslyuseless · Dec 22, 2013 at 05:01 PM 0
Share

I get this error : Object reference not set to an instance of an object

avatar image alexanderflink · Dec 22, 2013 at 07:04 PM 0
Share

Are the two scripts in two different Game objects?

avatar image infamouslyuseless · Dec 22, 2013 at 09:54 PM 0
Share

yes, they are

Follow this Question

Answers Answers and Comments

22 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

Related Questions

Scripting error! 1 Answer

Javascript not being updated, variables being overridden, but C# is fine 1 Answer

Assign a script to a variable? 1 Answer

Accessing Script From Other Script Causes Lag? 1 Answer

Prefabs and variables 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