Need help to loop an array of respawn prefabs after shooting them.
I'm trying to make a FPS scene with 5 targets in front of player spawning randomly at specific locations. I want to shoot the target and when I hit the target the next one will show up. This script is attached to my prefab Target.
public class pomperHit : MonoBehaviour {
Rigidbody rb;
Vector3 origLoc;
Quaternion origRot;
private TargetSpawner targetSpawner;
void Start()
{
rb = GetComponent<Rigidbody> ();
origLoc = transform.position;
origRot = transform.rotation;
targetSpawner = GetComponent<TargetSpawner> ();
}
void Update ()
{
if (Input.GetKeyDown ("r"))
{
transform.position = origLoc;
transform.rotation = origRot;
}
}
void OnCollisionEnter (Collision col)
{
if (col.gameObject.tag == "Bullet")
{
rb.AddRelativeForce(Vector3.forward * -250f);
targetSpawner.SpawnRoutine ();
Destroy (col.gameObject);
}
}
}
and this script attached to my spawner
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class TargetSpawner : MonoBehaviour {
public Transform[] spawnLocations;
public GameObject[] spawnPrefab;
public GameObject[] spawnClone;
void Start()
{
SpawnRoutine ();
}
public void SpawnRoutine()
{
int i = Random.Range (0, 5);
spawnClone [i] = Instantiate (spawnPrefab [i], spawnLocations [i].transform.position, Quaternion.Euler (0, 87f, 0)) as GameObject;
}
}
I called the SpawnRoutine() each time I had collision from OnCollisionEnter but Object reference not set to an instance of an object appearing in the console...Please help me...thanx in advance..
Your answer
Follow this Question
Related Questions
Spawn unique objects from the array 1 Answer
Comparing an ArrayList to the current object in an Arraylist being checked through a forEach Loop? 0 Answers
Loop just executes once 0 Answers
Move a character a set distance on NavMesh 0 Answers
problem when i spawn to many Enemy the game be slower 1 Answer