In Unity, How to Pass Values from One Script to Another

In Unity, how can I pass values from one script to another?

There are several ways to achieve this.

If you want the speed variable controlled by a component which is attached to a GameObject MyObject

public class SpeedController : MonoBehaviour
public float speed;
// maybe you want restrict this to have read access, then you should use a property instead

In other classes you can do:

GameObject go = GameObject.Find ("MyObject");
SpeedController speedController = go.GetComponent <SpeedController> ();
float courrentSpeed = speedController.speed;

Take care that there is one object named MyObject only otherwise things get messed up.

Alternatively you can define a SpeedController member in every class that needs access to speed and set a reference via drag and drop in Unity editor. You save the lookup then but of course this is pretty inconvenient if needed in many classes.


Another way is to create a singleton which holds the speed variable and have:

public class MyGlobalSpeedController {
private static MyGlobalSpeedController instance = null;
public static MyGlobalSpeedController SharedInstance {
get {
if (instance == null) {
instance = new MyGlobalSpeedController ();
}
return instance;
}
}
public float speed;
}

So all classes can access this:

float currentSpeed = MyGlobalSpeedController.SharedInstance.speed

As Jan Dvorak stated in comments section:

public class SpeedController : MonoBehaviour
public static float speed;

[Update]
Thanks to Jerdak. Yes Component.SendMessage should be definitely on the list:

go.SendMessage("GetFallingSpeed");

Again you need to have a reference to go like described in the first solution.

There are even more solutions to this problem. If you are thinking of game objects that are active in all scenes, you should have a look at Unity singleton manager classes

Pass Value to One Script to Another in Unity3d

If you're trying to add health to your second script, declare your health field as public. So that you can access its value in your first script.

public int health;

But I wouldn't do stuff like that. Expose this field by a property like:

public int Health 
{
get
{
return this.health;
}
set
{
this.health = value;
}
}

by default the health will be declared as

private int health;

Other scripts can't access private fields.
Also you need a reference to your second script. You can access this via:

public DataManager data;

You've to assign your second object into this field in your Unity Editor. Then
This way, you can access the field healthby calling data.health += 125 in your first script.

I don't know the specific thing in Unity, but I think you can also call your script by:

DataManager data = GetComponent<DataManager>();
data.health += 125;

Other method to get your other script is call it like that in your first script:

var secondScript = GameObject.FindObjectOfType(typeof(DataManager)) as DataManager;
secondScript.health += 125;

how to Access value of a script from another script and modify in unity#

First, you have to get the GameObject that the LeanRemapValue class is attached to, and then with
`.GetComponent().NewsMax you get the value and can modify it.

You can get the GameObject in the editor or with the GameObject.Find() method.

If a script inherits from MonoBehaviour it has to be attached to a GameObject and then the script acts as a component. .

pass int value from one script to another script in unity

Change line

public int score = 0;

to

public static int score = 0;

Note that you must only have one single instance of class firearrow, otherwise you might run into concurrency issues.

How to send a value from from one script to another on Unity

  1. PlayerPrefs.Save says you don't need to call it manually in OnApplicationQuit. Good.
  2. Call Application.Quit(); at last in OnApplicationQuit. Otherwise your changes may not be saved I guess.
  3. If you save the time like this: double sum_of_time_values = time_hour + time_minutes + time_seconds; wouldn't 20 minutes look the same as 20 seconds?
    Why not simply add the Time.deltaTime in Update() to a variable? Time will be in seconds.

Value from one script to another c#

When referencing a static variable you need to include the class name and a period before the variable.

private void LivesRespawn()
{
if (Livesvalue == 0)
{
SceneManager.LoadScene(0);
}
}

change to

private void LivesRespawn()
{
if (livesscript.Livesvalue < 1)
{
SceneManager.LoadScene(0);
}
}

How can I access a variable from another script in Unity?

Try this:

public class Particle : MonoBehaviour
{
public static Particle instance;
public float particleSize;
public Transform particle;

void Awake()
{
instance = this;
}
void Start()
{
particle.localScale *= particleSize;
}
}

public class Magnetic : MonoBehaviour
{

public Transform magnetic;

void Start()
{
magnetic.localscale *= Particle.instance.particleSize;
}
}

How can I change this value from a different script? UNITY C#

Do you want to change the speed for just one platform, or change for all platforms?

If you want the same speed for all platforms, you should make speed static.

public static float speed = 10.0f;

Then you can adjust the speed like this.

Platform.speed = 15.0f; //replace 15.0f with your desired speed.

If you want to change the speed for a specific platform, get the Platform component from a game object, and modify speed.

// assuming "platform" is of type gameObject
platform.GetComponent<Platform>().speed = 15.0f; // replace 15.0f with your desired speed.


Related Topics



Leave a reply



Submit