OK… I had two scripts running nicely, one instantiated an object and threw it into the air every x seconds, the other destroyed the object ready to instantiate a new one.
instantiate script-
using UnityEngine;
using System.Collections;
public class LavaThrower : MonoBehaviour
{
public Transform lavaPoint;
public Rigidbody lavaBallPrefab;
public static int ballNumbers = 0;
public int throwPower;
void Update()
{
if(ballNumbers <=0)
{
ThrowTheLavaBall();
}
}
void ThrowTheLavaBall()
{
ballNumbers += 1;
Rigidbody lavaBall;
lavaBall = Instantiate(lavaBallPrefab, lavaPoint.position, lavaPoint.rotation) as Rigidbody;
lavaBall.AddForce(lavaPoint.up * throwPower);
}
}
destroy script -
using UnityEngine;
using System.Collections;
public class DestroyLavaBall : MonoBehaviour
{
void Awake()
{
StartCoroutine(DestroyTheLavaBall());
}
IEnumerator DestroyTheLavaBall()
{
yield return new WaitForSeconds(6.5f);
LavaThrower.ballNumbers -= 1;
Destroy(gameObject); // Destroy Self
}
}
All was going well until I placed another spawner into my scene and then realised that because I’d used a static variable one script was overwriting the other.
I think (please correct me if wrong) that I’d be better off with one script per spawner that was handling the instantiate AND the destroy. Therefor removing the need for any static variables.
And that’s where my problem is. I’ve tried a few approaches (and to be honest) I’m screwing it up more than I’m making progress. So I’m looking for some suggestions as to how I write/structure the script.
Note that this is not the best way to do it. You’ll discover the principle of encapsulation later, and realise that this could still be made better. You are welcome to google the term to avoid heart ache. Or steam ahead and post back here when you come across the problem yourself. (Insert evil grin here)
You’re right that you need to stop using static for ball numbers if you want multiple spawners but whether you want to use one or two scripts is basically a design choice.
If you want to combine the scripts then you could add the following to LavaThrower
public void DestroyTheLavaBall(){
StartCoroutine(DestroyTheLavaBallRoutine());
}
IEnumerator DestroyTheLavaBallRoutine()
{
yield return new WaitForSeconds(6.5f);
ballNumbers -= 1;
Destroy(gameObject); // Destroy Self
}
Then since the new DestroyTheLavaBall() method is public you can run it instead of creating new DestroyLavaBall objects. For instance
public class Controller{
public GameObject lavaThrowerObj;
void Start(){
lavaThrowerObj.GetComponent<LavaThrower>().DestroyLavaBall();
}
}
You could also have two separate scripts. For instance you can use the following.
public class DestroyLavaBall : MonoBehaviour {
void Awake() {
StartCoroutine(DestroyTheLavaBall());
}
IEnumerator DestroyTheLavaBall() {
yield return new WaitForSeconds(6.5f);
gameObject.GetComponent<LavaThrower>().ballNumbers-=1;
Destroy(this); // Destroy Self
}
}
Then you would use it something like
public class Controller {
public GameObject lavaThrowerObj;
void Start() {
lavaThrowerObj.AddComponent<DestroyLavaBall>();
}
}