How can I Instantiate and do it in a random spot in a random spot?

I need to Instantiate a prefab in a random spot, those spots are marked by prefabs. I have tried to instantiate it normally, but even that won’t work.
Here is the instantiate code:

#pragma strict

var myString : String = "5";
var myInt : int = parseInt(myString);
var SpawnSpot : Transform;
var spawnMonster;
var choosingIsActive : boolean = true;
var Monster : Transform;

function Start () {


function Update () {

function OnGUI()
       myString = GUILayout.TextField(myString, 2);
       myString = Regex.Replace(myString, "[^0-9]", "");
	       if(GUI.Button(Rect(Screen.width/2.1, Screen.height/2.1, 106, 50), "Play!"))
	       		choosingIsActive = false;
				spawnMonster = Instantiate("PlayerController", transform.position, transform.rotation);

Here is the error I get:
Assets/Scripts/Temples Choosing Menu.js(28,59): BCE0023: No appropriate version of ‘UnityEngine.Object.Instantiate’ for the argument list ‘(String, UnityEngine.Vector3, UnityEngine.Quaternion)’ was found.

The main of the code to look at is line 28.

How can I fix this?

Also, is there a method that I can use for choosing a random spawn spot out of 6 placed around my map? I heard something about using Random.Range to figure it out, but I can’t understand how to use this.
Honestly, I need all the help I can get for this right now! I’m a new indie developer! I have searched everywhere online and the closest I have come to an answer is how to move it at to a random number/coordinate.

Thanks in advance!


PS. If possible, I need any code to be given in JavaScript (UnityScript). Thanks again!

  • Simply cast the object to gameobject.

    spawnMonster = (GameObject)Instantiate(“PlayerController”, transform.position, transform.rotation);

  • Your arguments aren’t correct Instantiate takes gameobject as first param, but you are passing a string “PlayerController”;

Here’s a simple example.

public GameObject prefab; //add from inspector

private void Start(){
    GameObject instance = (GameObject)Instantiate(prefab, new Vector3(1,2,3), Quaternion.Euler(0, 30, 0));

  • You can also create an empty gameObject using

    GameObject obj = new GameObject();

You can take 2 floats and use Random.Range function
here is the link

this is about invoke but here you can understand about this (how easy to use random.range).
Hope i am not wrong .

I am also making my first game so beginner you know but i hope i understand what this error says .
It didn’t find every parameter properly
“PlayerController” is a tag?
As you scripted i think you are very well known with unity(or this from a tutorial or a website) but i think you need to watch the instantiate funtion tutorial again . Their you can easily use game object named variable instant of “PlayerController”

Here is the link .

just take you 7 minutes total :slight_smile: