I'm relatively new to Unity and C# in general, so go easy on me.
So what I'm trying to achieve here is pretty straightforward. Basically, I have three specific Vector3 coordinates and I want my prefab to get instantiated at one of those three Vectors (chosen randomly). What I've tried hasn't exactly worked the way I wanted. Here's the current code:
public class MissileSpawnerScript : MonoBehaviour
{
// Variables:
public GameObject projectile;
public float timeBetweenShots;
private float shotTime;
Vector3[] position;
// Update is called once per frame
void Update()
{
Vector3 position = new Vector3(11.17f, Random.Range(-2.28f, 3.73f), 0);
if (Time.time >= shotTime)
{
Instantiate(projectile, position, transform.rotation);
shotTime = Time.time + timeBetweenShots;
}
}
}
I tried to figure this out a number of different ways as well but I was constantly getting this error message: "Object reference not set to an instance of an object" so I gave up and came here for valuable help. Also, if someone could explain with simple terms what this error means I'd highly appreciate it.
If you need more information or want me to be more specific or anything please let me know.
Thanks in advance for any kind of help. :)
EDIT: So, after all your suggestions, this is what I came up with (which actually worked):
public class MissileSpawnerScript : MonoBehaviour
{
// Variables:
public GameObject projectile;
public float timeBetweenShots;
private float shotTime;
Vector3[] position;
void Update()
{
Vector3[] position = { new Vector3 { x = 11.21f, y = -2.17f, z = 0 },
new Vector3 { x = 11.21f, y = -0.54f, z = 0},
new Vector3 { x = 11.21f, y = 1.14f, z = 0}};
Vector3 randomPosition = position[Random.Range(0, position.Length)];
if (Time.time >= shotTime)
{
Instantiate(projectile, randomPosition, transform.rotation);
shotTime = Time.time + timeBetweenShots;
}
}
}
Thanks again for everything.