0

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.

Nick
  • 1
  • 1
  • Have you assigned the reference of your projectile gameObject in the inspector? – TEEBQNE Jun 14 '21 at 18:36
  • Instead pick a random element from the `position` array you have? (Not a good name for multiple positions btw) only make sure it is visible in the Inspector (make it public or [SerializeField] ) – derHugo Jun 14 '21 at 21:09

1 Answers1

0

If you input two integers into Random.Range(), you would get an integer, not a float. So,

int randomInt = Random.Range(0, 5);

would only give you an integer. We could change this to access an element of an array:

Vector3 randomPos = positions[Random.Range(0, positions.Length)];

We could implement this into your script:

(This will only work if you created the array, and it has more than 0 elements)

public class MissileSpawnerScript : MonoBehaviour
{
    public GameObject projectile;
    public float timeBetweenShots;
    private float shotTime;
    Vector3[] position;
    void Update()
    {
        if (Time.time >= shotTime)
        {
            Instantiate(projectile, position[Random.Range(0, position.Length)], transform.rotation);
            shotTime = Time.time + timeBetweenShots;
        }
    }
}
gbe
  • 1,003
  • 1
  • 7
  • 23
  • That is **not** correct though. For the integer version the max value is **exclusive** so it should rather be `Random.Range(0, position.Length)` otherwise the last index is never returned ;) – derHugo Jun 14 '21 at 21:07
  • @derHugo I just updated the answer, with what I think you said. – gbe Jun 14 '21 at 23:11
  • Thank you everyone for the suggestions, I figured it out! If anyone wants/needs to see my code I'd be happy to share it with you. :) – Nick Jun 15 '21 at 11:58
  • @Nick would be best to post your own answer and **mark the answer you posted** as accepted, so people know what to do. – gbe Jun 15 '21 at 14:55