hello guys…
lets say the first object moves
and a second one moves too…
i want to have a random number x that my first object will increase its speed by x but my second will decrease it by the same number x.
but i want x number to be random
how can i do that…?
i thought about creating an empty object with a number randomizer in a range that i will select but how will my obstacle scripts will get this number???
Creating an empty object with a script to manage that value is one way to do it.
Create the empty game object and give it the following script:
public class GameManager : MonoBehaviour
{
static GameManager m_instance = null;
public static GameManager Instance
{
set
{
if (m_instance == null)
{
m_instance = value;
}
else
{
Debug.LogError("GameManager: You can't have more than 1 GameManager at a time!");
}
}
get
{
return m_instance;
}
}
public minimum = 1f; //assign in unity inspector those values
public maximum = 3f;
public float moveFactor = 0f;
void Start()
{
Instance = this; // here we assign our singleton, if we load more than one GameManager error will occur
moveFactor = Random.Range(minimum, maximum);
}
}
So now in your MoveableObject script you can simply call:
float value = GameManager.Instance.moveFactor;
Do note this is a oversimplified solution to your problem, because now your scripts reference the GameManager within your class, thus making it less reusable.
One way you could fix this is to give your GameManager the ability to spawn those MoveableObjects, and upon Instantiate(…) just set their moveFactor value. That way MoveableObject doesn’t need to know about GameManager at all.
Good luck!
thank you very much…
but i want the randomized number to be the same for each object.
this way i get different randomized number in my two objects!!
ok ok my false…
spawning them would be nice although i cant find a way to do this
This is strange, since GameManager is supposed to generate a random number only in Start() method, and only one instance is allowed. Could you please post what you have so far for your MoveableObject code?
Edit:
You could spawn them by creating the following method, either in your GameManager or your other script:
using System.Collections.Generic;
GameObject[] SpawnObjects(GameObject prefab, Vector3 spawnPoint, int number)
{
List<GameObject> spawnedObjects = new List<GameObject>();
for (int i = 0; i < number; i++)
{
GameObject obj = Instantiate (prefab, spawnPoint, Quaternion.identity) as GameObject;
spawnedObjects.Add(obj);
}
return spawnedObjects.ToArray();
}
Now you can call this method to obtain your spawned objects:
GameObject prefab = ... //assume this exists
Vector3 spawnPoint = Vector3.zero;
GameObject[] moveableObjects = SpawnObjects(prefab, spawnPoint, 2);
float moveFactor = 1f; //assume this exists
int i = 0;
foreach( GameObject obj in moveableObjects)
{
MoveableObject script = obj.GetComponent<MoveableObject>();
if (script == null)
{
Debug.LogError("Error: MoveableObject not found on gameObject!");
continue;
}
script.moveDirection = i % 2 == 0 ? moveFactor : -moveFactor;
i++;
}
That would be one way you could spawn any number of objects and assign them the moveDirection. Since you didn’t post any additional information how you game works or anything, I assumed this script would help you achieve what you want.
I hope this will help you a bit how to spawn objects. If you’re still having trouble with your scripting, I highly suggest downloading Unity example projects from Asset store. They are a great starting point in learning how to approach game development.