Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 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
1
Question by KingCrizzo · Sep 09, 2012 at 03:09 AM · variableaccess

Accessing a variable effected by a GUI slider from another scipt

I'm very new to this and have looked at a lot of similar questions and reference pages, but can't seem to wrap my head around it in the context of my project and make it work.

I have a GUI slider in my game that currently changes the mass of a projectile. I want to be able to access the value that is set by the slider from the script that creates the projectile so I can set the force of it based on the mass.

ResetButton.cs

 using UnityEngine;
 using System.Collections;
 
 public class ResetButton : MonoBehaviour 
 {
     public Rigidbody bullet;
     public float mass = 1.0f;
     void OnGUI () 
     {
         mass = GUI.VerticalSlider(new Rect(10,110,40,200),mass,20,1);
         bullet.mass = mass;
         
     }
 }

Shooter.cs

 using UnityEngine;
 using System.Collections;
 
 public class Shooter : MonoBehaviour 
 {
     public Rigidbody bullet;
     public float power = 1000f;
     void Update () 
     {
         if(Input.GetButtonUp ("Fire1"))
         {
             Rigidbody instance = Instantiate(bullet, transform.position, transform.rotation) as Rigidbody;
             Vector3 fwd = transform.TransformDirection(Vector3.forward);
             instance.AddForce (fwd * power);
         }
     }
 }

So how do I access the mass variable from the ResetButton script while in the Shooter script in order to modify the power variable?

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

1 Reply

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

Answer by aldonaletto · Sep 09, 2012 at 04:04 AM

Since you're using the GUI system, it's much easier to place the GUI code in the Shooter script:

... public class Shooter : MonoBehaviour { public float mass = 1.0f; public Rigidbody bullet; public float power = 1000f;

 void OnGUI () 
 {
    mass = GUI.VerticalSlider(new Rect(10,110,40,200),mass,20,1);
 }

 void Update () 
 {
    if(Input.GetButtonUp ("Fire1"))
    {
      Rigidbody instance = Instantiate(bullet, transform.position, transform.rotation) as Rigidbody;
      instance.mass = mass; // set the bullet mass
      Vector3 fwd = transform.TransformDirection(Vector3.forward);
      instance.AddForce (fwd * power);
    }
 }

}

EDITED: That's the easier solution in this case, for sure, but sometimes you definitely must read values from another script. To do that, you must have a reference to the script or to its owner object (the object the script is attached to). That's because several instances of this script may exist in the scene, thus Unity must know which one you actually want.
If ResetButton and Shooter are attached to the same object, you can get the script reference via GetComponent:

... public class Shooter : MonoBehaviour { public Rigidbody bullet; public float power = 1000f;

 void Update () 
 {
    if(Input.GetButtonUp ("Fire1"))
    {
      Rigidbody instance = Instantiate(bullet, transform.position, transform.rotation) as Rigidbody;
      ResetButton otherScript = GetComponent< ResetButton>(); // get the other script
      instance.mass = otherScript.mass; // copy the bullet mass from the other script
      Vector3 fwd = transform.TransformDirection(Vector3.forward);
      instance.AddForce (fwd * power);
    }
 }

} On the other hand, if ResetButton is attached to a different object, you should call GetComponent in the owner object using some reference to it (like otherObjectTransform.GetComponent, for instance), but there's a simpler and most popular way: create a public variable of the script type (which's the script name without extension) and drag the owner object to it in the Inspector:

... public class Shooter : MonoBehaviour { public Rigidbody bullet; public float power = 1000f; public ResetButton guiScript; // drag the ResetButton owner object here

 void Update () 
 {
    if(Input.GetButtonUp ("Fire1"))
    {
      Rigidbody instance = Instantiate(bullet, transform.position, transform.rotation) as Rigidbody;
      instance.mass = guiScript.mass; // copy the bullet mass from the other script
      Vector3 fwd = transform.TransformDirection(Vector3.forward);
      instance.AddForce (fwd * power);
    }
 }

}

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 KingCrizzo · Sep 09, 2012 at 04:15 AM 0
Share

Good idea, don't know why I didn't think of that. I guess I'll learn how to communicate between scripts some other time.

avatar image aldonaletto · Sep 09, 2012 at 01:06 PM 0
Share

That's the easiest way, for sure. Anyway, I edited my answer and included the alternatives to read mass from the ResetButton script - sooner or later, you'll have to learn this!

avatar image KingCrizzo · Sep 09, 2012 at 05:50 PM 0
Share

Thanks! That clears it up nicely. Will definitely use this as reference when I encounter this problem again.

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

Collision object and access to a script variable? 1 Answer

How do I access information (eg, variables, functions, properties) on other objects from inside a script? 2 Answers

My script accessing a variable from another script recieves an error 1 Answer

Can I make variables visible to other scripts without making them visible in the Inspector? 1 Answer

Cannot access a variable in 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