Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • 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
0
Question by Digital-Phantom · Jan 06, 2015 at 02:54 PM · javascriptinstantiaterandomgame objectspawnpoints

Can't figure out next step for this code ?

I'm trying to make a random object appear in a random location. Neither are actually truly random... I have 4 objects(weapons) and I want to spawn one at one of 3 spawn points.

I started out with this code to get my random spawn location-

 //Have 3 slots in my Inspector, into which I drag/drop my 3 empty game objects (SpawnPoints)
 
 #pragma strict
 
 public var spawnPoints : GameObject[]; //This allows me to make a list/group of Objects
 public var randomNum : int; //This denotes that the variable randomNum will be a whole number
 
 
 function Start()
 {
     randomNum = Random.Range(0,3); // generates random number between 0 and 3.
     
         transform.position = spawnPoints[randomNum].transform.position;
         //Assigns the number generated in the previous line to the 'spawnPoints' variable
         //Which then dictates which spawnPoint(location) is used (spawnPoint1, spawnPoint2, spawnPoint3)
     
 }

This worked great and gave me 3 different spawn locations. I've then tried to advance the script to pick one of 4 random (suedo random) objects which will then spawn at the above location. Gotten this far, but not sure how to tell it to pick one of the random objects?

 //Have 3 slots in my Inspector, into which I drag/drop my 3 empty game objects (SpawnPoints)
 
 #pragma strict
 
 public var spawnPoints : GameObject[]; //This allows me to make a list/group of Objects
 public var randomNum : int; //This denotes that the variable randomNum will be a whole number
 
 public var wepType : GameObject[];
 public var wepNumber : int;
 
 function Start()
 {
     randomNum = Random.Range(0,3); // generates random number between 0 and 3.
     
        transform.position = spawnPoints[randomNum].transform.position;
        //Assigns the number generated in the previous line to the 'spawnPoints' variable
        //Which then dictates which spawnPoint(location) is used (spawnPoint1, spawnPoint2, spawnPoint3)
 
        wepNumber = Random.Range(0,4); // generates random number between 0 and 4.

        wepType = GameObject[wepNumber];
 
        Instantiate(wepType, spawnPoints[randomNum].transform.position);
         
 }
 

I'm now getting these two error messages -

Assets/Scripts/RandomSpawnLocation.js(21,19): BCE0048: Type 'System.Type' does not support slicing.

Assets/Scripts/RandomSpawnLocation.js(23,20): BCE0023: No appropriate version of 'UnityEngine.Object.Instantiate' for the argument list '(UnityEngine.GameObject[], UnityEngine.Vector3)' was found.

Would really appreciate some help guys ?

(excuse all the // commenting. Its my way of trying to teach myself...lol)

Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Klarax · Jan 06, 2015 at 03:16 PM

it looks like you are using wepType in your instantiate func, which im pretty sure is an array.

 public var spawnPoints : GameObject[]; //This allows me to make a list/group of Objects
  public var randomNum : int; //This denotes that the variable randomNum will be a whole number
  
  public var wepType : GameObject[];
  public var wepNumber : int;
  
  function Start()
  {
         randomNum = Random.Range(0,3); // generates random number between 0 and 3.
      
         transform.position = spawnPoints[randomNum].transform.position;
 
         wepNumber = Random.Range(0,4); // generates random number between 0 and 4.
  
         GameObject chosenWeapon = GameObject[wepNumber];
 
         Instantiate(chosenWeapon, spawnPoints[randomNum].transform.position);
          
  }


have a go, im tired so might be off a bit

Comment
Add comment · Show 8 · 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 Digital-Phantom · Jan 06, 2015 at 03:24 PM 0
Share

now get this error -

Assets/Scripts/RandomSpawnLocation.js(21,19): UCE0001: ';' expected. Insert a semicolon at the end.

???

avatar image Klarax · Jan 06, 2015 at 03:31 PM 0
Share

ok

do this

  public var spawnPoints : GameObject[]; //This allows me to make a list/group of Objects
      public var randomNum : int; //This denotes that the variable randomNum will be a whole number
      
      public var wepType : GameObject[];
      public var wepNumber : int;
      
      function Start()
      {
             print ("A");
             randomNum = Random.Range(0,3); // generates random number between 0 and 3.
          
             print ("B");
             transform.position = spawnPoints[randomNum].transform.position;
     
             print ("C");
             wepNumber = Random.Range(0,4); // generates random number between 0 and 4.
      
             print ("D");
             GameObject chosenWeapon = GameObject[wepNumber];
     
             print ("E");
             Instantiate(chosenWeapon, spawnPoints[randomNum].transform.position);
              
      }
 


can see where its falling over then

might be because you got non matching brackets (one open and not closing one)

avatar image Digital-Phantom · Jan 06, 2015 at 03:51 PM 0
Share

Am I missing something? Surely if it won't compile, it won't play and I'm unable to see anything?

(every bracket I click has a highlighted one that corresponds)

avatar image Klarax · Jan 06, 2015 at 04:02 PM 0
Share

comment out each line one by one starting backwards until it compiles. might help find the line of code breaking.

Its strange tho. only changed two lines

wepType = GameObject[wepNumber];
to
GameObject chosenWeapon = GameObject[wepNumber];

and

Instantiate(wepType, spawnPoints[randomNum].transform.position);
to
Instantiate(chosenWeapon, spawnPoints[randomNum].transform.position);

avatar image Digital-Phantom · Jan 06, 2015 at 04:16 PM 0
Share

Line 25

GameObject chosenWeapon = GameObject[wepNumber];

Show more comments

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.

Follow this Question

Answers Answers and Comments

26 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Problem with Random.Range 1 Answer

Instantiating a random dropped consumable item from many cloned objects 1 Answer

multiple object in multiple spawn Point with out repeat 0 Answers

How to instantiate 2D object at fixed location? 1 Answer

Problem with Random.range 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