spawn randomly at specific positions

Hi!
I’m trying to make mice spawn randomly at certain positions but I’m struggling to find a good way to do so.
I thought I could define the positions with a vector2 but the instantiate function obviously doesn’t work with that.
Then I also found out that I can only assign a transform.position value to a gameobject and thus can’t really set specific transform positions in code. Unless there is another way to do it?

Now I wonder if this idea of setting spawn positions in code is feasible or if I should just give up with this and use gameobjects, or use Random.Range on X and Y and somehow exclude all values except for the ones at my target positions.

I’ve been stuck on this for a while so some help would be greatly appreciated :slight_smile:

public class MouseSpawnerScript : MonoBehaviour
{
    public GameObject mouse1;
    Vector2[] position ;

    // Start is called before the first frame update
    void Start()
    {
        Vector2 pos1 = new Vector2(0.9f, 0.7f);
        Vector2 pos2 = new Vector2(0, 0.7f);
        Vector2 pos3 = new Vector2(0.9f, 0.7f);
        Vector2 pos4 = new Vector2(-0.9f, 0.1f);
        Vector2 pos5 = new Vector2(0, 0.1f);
        Vector2 pos6 = new Vector2(0.9f, 0.1f);
        Vector2 pos7 = new Vector2(-0.9f, -0.5f);
        Vector2 pos8 = new Vector2(0, -0.5f);
        Vector2 pos9 = new Vector2(0.9f, -0.5f);
        Vector2[] position = {pos1, pos2, pos3, pos4, pos5, pos6, pos7, pos8, pos9};

        InvokeRepeating("Spawn", 1, 4);
    }

    // Update is called once per frame
    void Update()
    {
  
    }

    void Spawn()
    {
       
        Instantiate(mouse1, position[Random.Range(1,8)]);
    }
}

Not sure where you get this idea. Vector2 will be promoted to Vector3 and it will work just fine, with Z == 0

If you have a compiler error, it is because you are not giving Instantiate() everything it requires, in this case a rotation to go along with the position you are supplying.

Perhaps you intend to supply a third argument to Instantiate() that is Quaternion.identity ?

ALSO: beware that C# array indexing is zero-based, not 1-based.

If you have other compiler errors, remember: NOBODY memorizes error codes. The error code is absolutely the least useful part of the error. It serves no purpose at all. Forget the error code. Put it out of your mind.

The complete error message contains everything you need to know to fix the error yourself.

Always start with the FIRST error in the list, as sometimes that error causes or compounds some or all of the subsequent errors.

The important parts of the error message are:

  • the description of the error itself (google this; you are NEVER the first one!)
  • the file it occurred in (critical!)
  • the line number and character position (the two numbers in parentheses)

All of that information is in the actual error message and you must pay attention to it. Learn how to identify it instantly so you don’t have to stop your progress and fiddle around with the forum.

Hi Kurt! Thank you for the response. The above seemed to have been the problem so thank you for pointing it out. The error message only told me that it could not convert Vector2 to Transform but I assume that was just for the function without the rotation. After having added the Quaternion.identity it worked.

Thanks!

1 Like