- Home /
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?
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);
}
}
}
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.
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!
Thanks! That clears it up nicely. Will definitely use this as reference when I encounter this problem again.
Your answer
![](https://koobas.hobune.stream/wayback/20220613080812im_/https://answers.unity.com/themes/thub/images/avi.jpg)