- Home /
How can I change a float value of an instantiated object from another script?
I have a Spawn Manager script that is in charge of spawning waves of enemies. When a wave is completed, it is told to increase the rate at which it spawns enemies:
void WaveCompleted()
{
Debug.Log ("Wave Completed!");
state = SpawnState.COUNTING;
waveCountdown = timeBetweenWaves;
nextWave++;
newSpawnRate = spawnRate + .1f;
spawnRate = newSpawnRate;
The script I have for the instantiated enemies is pretty simple:
public float MoveSpeed = -2f;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void FixedUpdate () {
transform.Translate (new Vector3 (MoveSpeed, 0, 0)* Time.deltaTime);
}
void OnCollisionEnter(Collision col){
if (col.gameObject.tag == "Bullet")
Destroy (col.gameObject);
}
Since the Spawn Manager is already detecting when we've reached a new wave, I'd like to change the MoveSpeed float from this script similar to the way it effects the spawn rate. When I try to reference it, it seems to break the speed and the enemies don't move across the screen at all and start spawning on top of each other. I don't know if I'm going about referencing the value correctly as I'm just starting coding. Any help would be greatly appreciated.
Answer by naderlabbad309 · Feb 27, 2019 at 05:51 PM
go to your script 1 and put this this to call you script 2 and float dont forget put name yourscript 2 time
public yourscript yourscript ;
this how call value
yourscript .yourscript = 1
Answer by WarmedxMints · Feb 27, 2019 at 05:52 PM
Simply get the script and modify it when you spawn them.
var go = Instantiate(EnemyPrefab);
var enemyScript = go.GetComponent<MyEnemyClass>();
enemyScript.MoveSpeed = 5f;
Answer by TheOnlyMike675 · Feb 27, 2019 at 06:55 PM
Hi there! If I understand you correctly, you may need to loop through the existing enemy objects and change the MoveSpeed variable that way.
GameObject[] enemies;
enemies = GameObject.FindGameObjectsWithTag("Enemy");
foreach(GameObject enemy in enemies)
{
enemy.GetComponent<nameOfScriptOnEnemy>().MoveSpeed = newValue;
}
This would only affect the speed of the already existing enemy objects I believe. In this case it may be easier to use global variables. Create an Empty GameObject, call it globals, and place the following code inside the monobehaviour of a script (call the script "globals") and add it to the globals object.
public static globals instance = null;
public static float MoveSpeed;
void Awake()
{
if (instance == null)
{
instance = this;
}
else
{
if (instance != this)
{
Destroy(gameObject);
}
}
DontDestroyOnLoad(gameObject); //Persists between scenes
}
void Start()
{
MoveSpeed = -2f;
}
The code in the Awake method is called the singleton pattern and it makes it so that there is only one of the globals object at a given time.
Then when you want to access and/or change MoveSpeed, you call it with globals.MoveSpeed in a script.
Try to avoid using globals for everything, but in some situations they're very useful. I'm also slightly new to C# so we're in this together!
I hope that helps!