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.
Answer by Sectah · May 25, 2016 at 06:31 PM
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. :)
Answer by tanoshimi · May 25, 2016 at 05:44 PM
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?
It doesn't let me access enemy$$anonymous$$ovement.speed unless the variable is static. I get this error:
error CS0120: An object reference is required to access non-static member `enemy$$anonymous$$ovement.speed'
Your answer
Follow this Question
Related Questions
How to Change the Variables of an Instantiated Object? 0 Answers
Assign variables on Instantiation? 0 Answers
Infinite Instantiation 2 Answers
Workflow ideas needed 0 Answers