• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
Question by The_Unity_Game_Developer · Jan 03, 2015 at 04:20 PM · instantiaterandomrandom.rangerange

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!

Comment

People who like this

0 Show 0
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image

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

Comment

People who like this

0 Show 6 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image The_Unity_Game_Developer · Jan 03, 2015 at 05:01 PM 0
Share

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.

avatar image YoungDeveloper · Jan 03, 2015 at 06:07 PM 0
Share

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));





avatar image The_Unity_Game_Developer · Jan 03, 2015 at 07:04 PM 0
Share

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!

avatar image The_Unity_Game_Developer · Jan 03, 2015 at 08:19 PM 0
Share

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));
avatar image The_Unity_Game_Developer · Jan 03, 2015 at 08:21 PM 0
Share

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;

Show more comments
avatar image

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 :)

Comment

People who like this

0 Show 0 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges