Change value of a static variable multiple times in one script

I’m spawning a number of different sized enemies that move at different speeds. I control the speed with a variable called speed in another script.

I’m trying to instantiate multiple different version of said enemy with different speeds. As seen below:

level1GM.cs

using UnityEngine;
using System.Collections;

public class level1GM : MonoBehaviour {

	public GameObject enemy;
	Vector3 size9, size17;

	void Start () {
		size9 = new Vector3 (0.0f, 0.0f, 8.0f);
		size17 = new Vector3 (0.0f, 0.0f, 16.0f);

		enemyMovement.speed = 6f;
		enemy.transform.localScale += size9;
		Instantiate (enemy, new Vector3(-20.0f, 0.0f, 5.5f), Quaternion.identity);
		enemy.transform.localScale -= size9;

		enemy.transform.localScale += size9;
		Instantiate (enemy, new Vector3(20.0f, 0.0f, -5.5f), Quaternion.Euler(0.0f, 180.0f, 0.0f));
		enemy.transform.localScale -= size9;

		enemyMovement.speed = 4f;
		Instantiate(enemy, new Vector3(9.5f, 0.0f, 25.0f), Quaternion.Euler(0.0f, 90.0f, 0.0f));
		Instantiate(enemy, new Vector3(2.5f, 0.0f, 25.0f), Quaternion.Euler(0.0f, 90.0f, 0.0f));
		Instantiate(enemy, new Vector3(-1.5f, 0.0f, 25.0f), Quaternion.Euler(0.0f, 90.0f, 0.0f));
		Instantiate(enemy, new Vector3(-3.5f, 0.0f, 25.0f), Quaternion.Euler(0.0f, 90.0f, 0.0f));
		Instantiate(enemy, new Vector3(-5.5f, 0.0f, 25.0f), Quaternion.Euler(0.0f, 90.0f, 0.0f));
		Instantiate(enemy, new Vector3(-7.5f, 0.0f, 25.0f), Quaternion.Euler(0.0f, 90.0f, 0.0f));
		Instantiate(enemy, new Vector3(-9.5f, 0.0f, 25.0f), Quaternion.Euler(0.0f, 90.0f, 0.0f));

		enemyMovement.speed = 4.2f;
		Instantiate(enemy, new Vector3(-9.5f, 0.0f, 32.0f), Quaternion.Euler(0.0f, 90.0f, 0.0f));
		Instantiate(enemy, new Vector3(-2.5f, 0.0f, 32.0f), Quaternion.Euler(0.0f, 90.0f, 0.0f));
		Instantiate(enemy, new Vector3(1.5f, 0.0f, 32.0f), Quaternion.Euler(0.0f, 90.0f, 0.0f));
		Instantiate(enemy, new Vector3(3.5f, 0.0f, 32.0f), Quaternion.Euler(0.0f, 90.0f, 0.0f));
		Instantiate(enemy, new Vector3(5.5f, 0.0f, 32.0f), Quaternion.Euler(0.0f, 90.0f, 0.0f));
		Instantiate(enemy, new Vector3(7.5f, 0.0f, 32.0f), Quaternion.Euler(0.0f, 90.0f, 0.0f));
		Instantiate(enemy, new Vector3(9.5f, 0.0f, 32.0f), Quaternion.Euler(0.0f, 90.0f, 0.0f));

		enemyMovement.speed = 7f;
		enemy.transform.localScale += size17;
		Instantiate (enemy, new Vector3(-1.5f, 0.0f, 37.0f), Quaternion.Euler(0.0f, 90.0f, 0.0f));
		enemy.transform.localScale -= size17;
	}
}

enemyMovement.cs

using UnityEngine;
using System.Collections;

public class enemyMovement : MonoBehaviour {

	public static float speed = 1f;

	// Use this for initialization
	void Start () {
		Destroy (gameObject, 15f);
	}
	
	// Update is called once per frame
	void Update () {
		transform.Translate (speed * Time.deltaTime, 0.0f, 0.0f);
	}

	void OnCollisionEnter (Collision col){
		if (col.collider.tag == "Player") {
			Debug.Log ("Death");
		}
	}
}

But it will only use the last enemyMovement.speed because of course it’s in start. But how would I go about having different speeds of different instantiated objects? Would I need a variety of different prefabs?

Thank you.

It’s nothing to do with it being set in Start(). It’s because you’ve declared speed as static. Why did you do this if you want different enemies to have different speeds?

Managed to solve it with help from @tanoshimi

Got rid of static from the other script then used GetCompenent instead for the following:

using UnityEngine;
using System.Collections;

public class level1GM : MonoBehaviour {

	public GameObject enemy;
	Vector3 size9, size17;
	private enemyMovement enemyMovementScript;

	void Start () {
		size9 = new Vector3 (0.0f, 0.0f, 8.0f);
		size17 = new Vector3 (0.0f, 0.0f, 16.0f);

		enemyMovementScript = enemy.GetComponent<enemyMovement>();

		enemyMovementScript.speed = 6f;
		enemy.transform.localScale += size9;
		Instantiate (enemy, new Vector3(-20.0f, 0.0f, 5.5f), Quaternion.identity);
		enemy.transform.localScale -= size9;

		enemy.transform.localScale += size9;
		Instantiate (enemy, new Vector3(20.0f, 0.0f, -5.5f), Quaternion.Euler(0.0f, 180.0f, 0.0f));
		enemy.transform.localScale -= size9;

		enemyMovementScript.speed = 4f;
		Instantiate(enemy, new Vector3(9.5f, 0.0f, 25.0f), Quaternion.Euler(0.0f, 90.0f, 0.0f));
		Instantiate(enemy, new Vector3(2.5f, 0.0f, 25.0f), Quaternion.Euler(0.0f, 90.0f, 0.0f));
		Instantiate(enemy, new Vector3(-1.5f, 0.0f, 25.0f), Quaternion.Euler(0.0f, 90.0f, 0.0f));
		Instantiate(enemy, new Vector3(-3.5f, 0.0f, 25.0f), Quaternion.Euler(0.0f, 90.0f, 0.0f));
		Instantiate(enemy, new Vector3(-5.5f, 0.0f, 25.0f), Quaternion.Euler(0.0f, 90.0f, 0.0f));
		Instantiate(enemy, new Vector3(-7.5f, 0.0f, 25.0f), Quaternion.Euler(0.0f, 90.0f, 0.0f));
		Instantiate(enemy, new Vector3(-9.5f, 0.0f, 25.0f), Quaternion.Euler(0.0f, 90.0f, 0.0f));

		enemyMovementScript.speed = 4.2f;
		Instantiate(enemy, new Vector3(-9.5f, 0.0f, 32.0f), Quaternion.Euler(0.0f, 90.0f, 0.0f));
		Instantiate(enemy, new Vector3(-2.5f, 0.0f, 32.0f), Quaternion.Euler(0.0f, 90.0f, 0.0f));
		Instantiate(enemy, new Vector3(1.5f, 0.0f, 32.0f), Quaternion.Euler(0.0f, 90.0f, 0.0f));
		Instantiate(enemy, new Vector3(3.5f, 0.0f, 32.0f), Quaternion.Euler(0.0f, 90.0f, 0.0f));
		Instantiate(enemy, new Vector3(5.5f, 0.0f, 32.0f), Quaternion.Euler(0.0f, 90.0f, 0.0f));
		Instantiate(enemy, new Vector3(7.5f, 0.0f, 32.0f), Quaternion.Euler(0.0f, 90.0f, 0.0f));
		Instantiate(enemy, new Vector3(9.5f, 0.0f, 32.0f), Quaternion.Euler(0.0f, 90.0f, 0.0f));

		enemyMovementScript.speed = 7f;
		enemy.transform.localScale += size17;
		Instantiate (enemy, new Vector3(-1.5f, 0.0f, 37.0f), Quaternion.Euler(0.0f, 90.0f, 0.0f));
		enemy.transform.localScale -= size17;
	}
}

Thanks for the help. :slight_smile: