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 The-W.A.T.Z.R · Apr 22, 2014 at 05:25 PM · javascriptprefabschangesystemspawnpoints

Multi Random Spawn Enemies / Item

I can not think of what I should do. I need help in the picture you can see that I have spawn points and that I can decide how many I want, of a completely simple empty object that is my spawn point. and you will see that I have frefabs for items or enemies. but what I want to change that for each element in spwan point I want applicants feature prefabs.

alt text

I want it to look like this """this photo is made in paint""" XD

alt text

you might already understand what I want.

that I may be able to place different things in the different spawn. so that not all spawns, spawns from the same frefabs list

here is the Spawn E&I script.

 var spawnPoints : Transform[];  // Array of spawn points to be used.
 var Prefabs : GameObject[]; // Array of different Enemies or Item that are used.

 function Start(){
     Spawn();
 }

 function Spawn(){ 

       var obj : GameObject = Prefabs[Random.Range(0, Prefabs.length)]; // Randomize the different enemies or Item to instantiate.
       var pos: Transform = spawnPoints[Random.Range(0, spawnPoints.length)];  // Randomize the spawnPoints to instantiate enemy or Item at next.

       Instantiate(obj, pos.position, pos.rotation); 
 }

:D BIG ThX to you who will help me, Really appreciate it :)

it looks.png (8.3 kB)
i want.png (12.1 kB)
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

3 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by OtsegoDoom · Apr 22, 2014 at 05:53 PM

You'll want to set up a class for your spawn points.

 var spawnPoints : SpawnPoints;
 
 class SpawnPoints {
    var spawnTransform : Transform;
    var prefabs : GameObject[];
 }

You can then access each element of the class like so:

 var obj : GameObject = spawnPoints[x].prefabs[Random.Range(0, spawnPoints[x].prefabs.length)];

Ex. spawnPoints.spawnTransform = gameObject.transform;

Comment
Add comment · Show 16 · 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-W.A.T.Z.R · Apr 22, 2014 at 06:04 PM 0
Share

how would it look like in the script

Big thx to you

avatar image OtsegoDoom · Apr 22, 2014 at 06:14 PM 0
Share

Try it and find out :D

It should look the same actually, with one exception. Ins$$anonymous$$d of the line: Element 0 Spawn1 (Transform)

It would look like: Element 0 Spawn Transform Spawn1 (Transform)

avatar image The-W.A.T.Z.R · Apr 22, 2014 at 06:18 PM 0
Share

i got this error :P

Assets/Spawn E&I.js(4,5): BCE0089: Type 'Spawn E&I' already has a definition for 'spawnPoints'.

avatar image aceX · Apr 22, 2014 at 06:23 PM 0
Share

That would probably mean you've already declared spawnPoints somewhere else in the script. $$anonymous$$ake sure you only declare it once. Cheers

avatar image Scribe · Apr 23, 2014 at 11:31 AM 1
Share

Something like this maybe:

 var spawnPoints : SpawnPoint[];
 
 class SpawnPoint{
     var spawnTransform : Transform;
     var prefabs : GameObject[];
 }
 
 function Start(){
     Spawn();
 }
 
 function Spawn(){
     var spawn : SpawnPoint = spawnPoints[Random.Range(0, spawnPoints.length)];
     var obj : GameObject = spawn.prefabs[Random.Range(0, spawn.prefabs.length)];
     Instantiate(obj, spawn.spawnTransform.position, spawn.spawnTransform.rotation); 
 }
Show more comments
avatar image
0

Answer by delorean225 · Apr 23, 2014 at 04:47 PM

The question's already answered, but just a reminder: RandomRange is inclusive/exclusive. So if you make a float value that's a Random.Range(0, 5) It can be any number from 0 to 4. An Int would be 0 to 4.9999999999999999999999999... you get the point. So to make the whole list, you need to say Random.Range(0, ValueYouWant.length + 1) to include the top.

Comment
Add comment · 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-W.A.T.Z.R · Apr 23, 2014 at 05:07 PM 0
Share

it surely can not be, i don't understand you there ? :3

avatar image delorean225 · Apr 23, 2014 at 05:09 PM 0
Share

To put it more simply, Random.Range isn't picking a number from x to y. It's picking a number that's greater than or equal to x and any number that's lower than y. Pretty much, if you leave it as x to y, it is going to pick numbers from x to y-1. You have to choose x and y+1 to get the full size of y.

avatar image Scribe · Apr 23, 2014 at 05:10 PM 0
Share

Wrong way round:

RandomRange(0, 5) -> either 0, 1, 2, 3, 4.

RandomRange(0, parseFloat(5)) -> between 0 and 5 (including 4.9999)

if you have a list of length 3 then you want to use RandomRange(0, 3), if you used RandomRange(0, 4) //length + 1 it would occasionally return 3 and index 3 doesn't exist in a list of length 3 as they are zero-indexed.

avatar image Scribe · Apr 23, 2014 at 05:27 PM 0
Share

your script is fine, delorean is incorrect, you would get some errors occasionally if you did Random.Range(0, ValueYouWant.length + 1) as he suggested.

avatar image delorean225 · Apr 23, 2014 at 05:33 PM 0
Share

Hmm. The scripting reference says you're right... I probably was looking at a generic JS thing. Sorry.

Show more comments
avatar image
0

Answer by Candyman89 · May 07, 2014 at 08:42 AM

Sorry, I have a question... when you set the Random.Range, shouldn't be the screen's width and height the parameters? So the items and enemies appears inside the screen... I'm pretty new at this... greetings :)

Comment
Add comment · Show 1 · 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-W.A.T.Z.R · Aug 05, 2014 at 01:12 AM 0
Share

no, in this script you need to place out spawns where they are co$$anonymous$$g from, they dont random spawn in the scene/map that only random choose items/enemies to be dropped from the spawn.

hope this was the answer you're looking for, if I understand your question right

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

25 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

Related Questions

creating an object using javascript 1 Answer

change a material from multiple materials 1 Answer

change skybox via script help ? 1 Answer

How to Implement System.StringBuilder into JavaScript? 2 Answers

Setting Scroll View Width GUILayout 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