So i’ve looked around on here for a couple of days, and I can’t seem to get anything to work. I have a class that controls an enemy spawn. The enemies are just spheres. The C# script uses a couroutine to creat the spheres. I want the coroutine to be called when the player collides with a specific box collider with rigidbody.
My Spawn script that is attached to an empty game object:
using UnityEngine;
using System;
using System.Collections;
public class Spawn : MonoBehaviour {
public GameObject[] sphere;
public static event Action created;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
IEnumerator createSphere(float seconds) {
yield return new WaitForSeconds(seconds);
GameObject copy = Instantiate(sphere[(int)UnityEngine.Random.Range(0.0f,4.0f)], transform.position, Quaternion.identity) as GameObject;
copy.rigidbody.velocity = UnityEngine.Random.onUnitSphere * UnityEngine.Random.Range (5, 10);
if(created != null) {
created();
}
//direction = Random.onUnitSphere;
//StartCoroutine (createSphere(seconds));
}
public void startSpawn() {
StartCoroutine(createSphere(5f));
}
}
My SpawnTrigger script that is attached to the box collider with rigidbody:
using UnityEngine;
using System.Collections;
public class SpawnTrigger : MonoBehaviour {
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
void OnCollisionEnter(Collision collision) {
if (collision.gameObject.tag == "Player") {
Spawn.startSpawn();
}
}
}
I’m getting this error: An object reference is required for the non-static field, method, or property Spawn.startSpawn(); which is near the bottom of the second script.
Any help? Thanks!
Corey