- Home /
How can I Instantiate and do it in a random spot in a random spot?
Hi! 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()
{
if(choosingIsActive)
{
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!
-The_Unity_Game_Developer.
PS. If possible, I need any code to be given in JavaScript (UnityScript). Thanks again!
Answer by YoungDeveloper · Jan 03, 2015 at 04:21 PM
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();
http://docs.unity3d.com/ScriptReference/Object.Instantiate.html
Thank you so much! Will it work for the position and rotation if I use a variable? EG: SpawnSpot.transfom.position, SpawnSpot.transform.rotation
Also, now all I need is an answer to my second question. How do I do the random spawn spot thing? Please feeder to original question for more info.
Yes of course it will, 3rd param takes a quaternion, i used .Euler to use simple vector3 as a rotation, as it's more understandable.
There are two easy ways to do it, either create a public transform array and fill it with empty gameobjects which will server as spawnpoints. Then use Random.Range to select random array slot and take that position.
public Transform[] spawnPoints; //define and add from inspector
public void Spawn(){
int indx = Random.Range(0,spawnPoints.Length-1);
Vector3 spawnPosition = spawnPoints[indx].position;
//spawn
}
Either use actual Random spawning, but then again just use Random.Range()
Instantiate(prefab, new Vector3(Random.Range(0f,10f),0,Random.Range(0f,10f)), Quaternion.Euler(0, 30, 0));
Ok that's perfect! Thanks! Just one more thing. How would these code lines be written in javascript? I notice they're in CSharp. Would they just be the same in UnityScript as they would be in C#? Thanks again!
Ok. Another problem. The code I used for the instantiate was the one using .Euler and new Vector3. However, these make the guy spawn at a different point, spawning him in 'the void'. How can I make the position and the rotation act with the SpawnPoints?
I'm using this code version you made:
GameObject instance = (GameObject)Instantiate(prefab, new Vector3(1,2,3), Quaternion.Euler(0, 30, 0));
I forgot to mention, for the spawn points I'm using the public Transform[] spawnPoints;
and for the random.range I'm using int indx = Random.Range(0,spawnPoints.Length-1); Vector3 spawnPosition = spawnPoints[indx].position;
Answer by JeevanjotSingh · Jan 04, 2015 at 04:33 PM
You can take 2 floats and use Random.Range function here is the link
http://unity3d.com/learn/tutorials/modules/beginner/scripting/invoke
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 .
http://unity3d.com/learn/tutorials/modules/beginner/scripting/instantiate
just take you 7 minutes total :)
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
How To Random Range a Float? 2 Answers
Instantiate issues 1 Answer
How do Ensure that a series of int's are never the same? 1 Answer
Random.range multiple instantiations without repetition 1 Answer