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
1
Question by Steve Cogbill · Mar 03, 2014 at 07:04 PM · triggerrandomspawnontriggerenter

Spawn Random Item On Trigger Enter

I have 3 different items and a box collider. When I walk into the collider I want to be given one of the 3 items at random. When I enter the trigger I want the item I get to spawn in front of me. I'm guessing that I need to make a empty gameObject as a spawnpoint. So I need to figure out how to get a random item from the collider and have it appear at the spawnpoint. Can anyone give me any help? That would be great! Steve

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

2 Replies

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

Answer by Dblfstr · Mar 03, 2014 at 07:36 PM

You will need an array or list of your 3 items (as prefabs).

 public GameObject[] itemPrefabs;

Drag your 3 items into the GameObject array in the inspector.

Then you need your trigger/spawn script

 OnTriggerEnter(Collider other){
  //Gonna spawn stuff
 }

Might need to check if the collider is the player

 if(other.tag == "Player"){
 //Ok, now we spawn suff
 }

You will need a spawn function. You could do this inside the trigger function but here is a standalone function..

 void Spawn(){
 Instantiate(itemPrefabs[Random.Range(0,itemPrefabs.Length)],transform.position, Quaternion.identity);
 }

Then add a bool to see if the spawner has spawned.

 bool hasSpawned = false;

Attache this to an empty gameobject or other gameObject with a trigger collider. The item will spawn at the location of the trigger when the player walks into it. Total script:

 Using UnityEngine;
 Using System.Collections; //for random numbers
  
 public class itemSpawner : MonoBehaviour
 {
 
     bool hasSpawned = false;
    public GameObject[] itemPrefabs;
  
    void OnTriggerEnter(Collider other)
    {
      if(other.tag == "Player")
      {
         if(!hasSpawned)
         {
             hasSpawned = true;
             Spawn();
             
         }
      }
    }
  
    void Spawn()
    {
     
       Instantiate(itemPrefabs[Random.Range(0,itemPrefabs.Length)],transform.position, Quaternion.identity);
    }
 }
Comment
Add comment · Show 18 · 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 Steve Cogbill · Mar 04, 2014 at 08:30 PM 0
Share

Thanks for the script. When I add it in, it comes up with a few errors:

(18,60)The member 'System.Array.Length' cannot be used as method or delegate

(18,40)The best overloaded method match for 'UnityEngine.Random.Range(float, float)' has some invalid arguments

(18,40)Argument '#2' cannot convert 'object' expression to type 'float'

I also had to change the first line to:

 public class itemSpawner : $$anonymous$$onoBehaviour

Otherwise it was a parcing error

avatar image Dblfstr · Mar 05, 2014 at 01:44 PM 0
Share

I did type this on the fly, so I can accept the errors. You can use GetLength, ins$$anonymous$$d of length. And you could add an 'f' after 0, in random.range, to signify it is a float. I will make the edits to the script above. However, the logic is sound, and should work as you asked.

avatar image whydoidoit · Mar 05, 2014 at 01:45 PM 0
Share

Don't add the f for float - it's an array, you do not want a float out of it.

Change it to:

     itemPrefabs.Length
avatar image whydoidoit · Mar 05, 2014 at 01:53 PM 1
Share

This is the correct line for Instantiate:

    Instantiate(itemPrefabs[Random.Range(0,itemPrefabs.Length)],transform.position, Quaternion.identity);
avatar image wibble82 · Mar 05, 2014 at 10:10 PM 1
Share

fyi - in answer to 'random.range' expects two floats. Random.Range comes in 2 flavours:

The 'int' version: Random.Range(int $$anonymous$$, int max). Returns a value from $$anonymous$$ to max-1. Great for randomly choosing an index in an array.

The 'float' version: Random.Range(float $$anonymous$$, float max). Returns a floating point value from $$anonymous$$ to max. Good for randomly choosing positions and all sorts of fanciful things :)

Show more comments
avatar image
0

Answer by kingames123456789 · Jul 19, 2017 at 01:07 PM

In this line :

  void Spawn(){ Instantiate(itemPrefabs[Random.Range(0,itemPrefabs.Length)],transform.position,

Quaternion.identity); }

How shuld I change it if I want to transform the position to another game object that is not in the array

Comment
Add comment · Show 2 · 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 kingames123456789 · Jul 19, 2017 at 01:08 PM 0
Share

it can be in the array , just to make it teleport to that object

avatar image kingames123456789 · Jul 19, 2017 at 01:32 PM 0
Share

Something like this :

void Spawn(){ Instantiate(itemPrefabs[Random.Range(0,itemPrefabs.Length)],transform.position, spawner.transform.position); }

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

24 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

Related Questions

Collision, but then only affect ONE of the gameObjects? 2 Answers

Trigger Spawning? 1 Answer

Random spawn of object on trigger 3 Answers

[solved] respawn when destroyed 1 Answer

Randomly Place Cubes In Viewport Without Overlap 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