- Home /
Same Random.Range Value for 2 Objects.
Hi, So I have 2 objects which will need to move at the same random generated speed but i can't seem to figure how to get the same random number for the 2 objects.
Answer by Luis0n7i · Aug 25, 2020 at 06:03 PM
Hello :) this is my first time answering a question. I hope this is useful. It's best if you can elaborate more on what you're trying to do (have you already attempted to solve the problem? If so, how? You could share your code so it's easier to help you!)
For getting two or more scripts to communicate between each other, from what I understand there are a number of ways to do this in Unity:
a) The most common would maybe be to call one script in another by referencing the game object with the original Random value. Each script component is a class with methods and variables inside, which you can call as long as they're public. Let's assume we have two objects, Object1 and Object2. Object1 holds the original Random value, and Object2 calls it in its own script. You could do the following, for Object 1 with a script called "Example1":
using UnityEngine;
public class Example1 : MonoBehaviour
{
public float speed;
public float speedMin = 1f;
public float speedMax = 5f;
void Start() {
speed = Random.Range(speedMin, speedMax);
}
void Update() {
transform.Translate(Vector3.forward * speed * Time.deltaTime);
}
}
For Object2, with a script called "Example2":
using UnityEngine;
public class Example2 : MonoBehaviour
{
private Example1 example1;
void Start() {
GameObject object1 = GameObject.Find("Object1");
example1 = object1.GetComponent<Example1>();
}
void Update()
{
transform.Translate(Vector3.forward * example1.speed * Time.deltaTime);
}
}
b) The simplest way in this case would be to simply use a static method by doing the following: For Object 1 the script stays almost the same, but we make the "speed" variable static.
using UnityEngine;
public class Example1 : MonoBehaviour
{
public static float speed;
public float speedMin = 1f;
public float speedMax = 5f;
void Start() {
speed = Random.Range(speedMin, speedMax);
}
void Update()
{
transform.Translate(Vector3.forward * speed * Time.deltaTime);
}
}
Then in the Example2 script for Object2 we just call the variable this way:
using UnityEngine;
public class Example2 : MonoBehaviour
{
void Update()
{
transform.Translate(Vector3.forward * Example1.speed * Time.deltaTime);
}
}
Notice we just skipped all the process of finding the game object, adding the component, etc. However, now if Example2 wanted to change the value of "speed" at some point, it would not be possible, given that the variable is static.
For more info on this I suggest you check out this video by Sebastian Lague, as well as the Unity tutorials for GetComponent and Statics.
Good luck and hope this helps! Like I said, I'm really new to answering questions, and I feel this might have been too convoluted and maybe I'm wrong in some parts, but let me know if it worked for you :)
Thank you this works and it was easier than System.random in my case.
Answer by Captain_Pineapple · Aug 25, 2020 at 05:32 PM
Hey there, i'd suggest 2 different solutions: Either you create some kind of publicly accessable class instance which generates the Path and your objects can then poll for the values or you use the System.Random(seed)
class to create a generator Object with a seed that is the same for both objects. Since computers always just use pseudo randomness a generator with the same seed as another generator will always result in the same order of random numbers. Then you just have to make sure that both objects have the same seed but that should be simpler.