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 Tom_1132 · Mar 13, 2014 at 01:57 PM · collisioninstantiateprefabplayer

Instantiating random prefabs when the player passes through a trigger

So basically for this project my character will be moving in 2D space (endless runner style, think Jetpack Joyride but with movement controlled by player input).

When the player passes through a trigger I need to instantiate a random prefab (wall tile sets in this case) to spawn at a designated spawn point. At the moment, the random prefab is instantiating where I need it to, but it happens instantly (as soon as I enter Play Mode).

I need the prefabs to instantiate ONLY when the player passes through the 2D Collider I have placed.

The following code is the result of my tweaking.. not correct obviously but I think you can see what I was trying to do. (Just to preface - I'm a game artist with limited programming knowledge, the fact I got to this stage is quite frankly a miracle, the solution to this is probably really obvious to the programmers out there)

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 
 public class StartingAreaTileSpawner : MonoBehaviour {
 
     public GameObject SpawnPosition = null;
 
     List<GameObject> prefabList = new List<GameObject>();
     public GameObject Prefab1;
     public GameObject Prefab2;
     public GameObject Prefab3;
     public GameObject Prefab4;
     public GameObject Prefab5;
     public GameObject Prefab6;
     public GameObject Prefab7;
     public GameObject Prefab8;
     public GameObject Prefab9;
     public GameObject Prefab10;
     public GameObject Prefab11;
     public GameObject Prefab12;
     public GameObject Prefab13;
 
     
     // Use this for initialization
     void Start () {
 
         prefabList.Add (Prefab1);
         prefabList.Add (Prefab2);
         prefabList.Add (Prefab3);
         prefabList.Add (Prefab4);
         prefabList.Add (Prefab5);
         prefabList.Add (Prefab6);
         prefabList.Add (Prefab7);
         prefabList.Add (Prefab8);
         prefabList.Add (Prefab9);
         prefabList.Add (Prefab10);
         prefabList.Add (Prefab11);
         prefabList.Add (Prefab12);
         prefabList.Add (Prefab13);
 
         int prefabIndex = UnityEngine.Random.Range (0, 13);
 
 
 
 
     
     }
 
 
 
     
     // Update is called once per frame
     void Update () {
 
     
     }
 
     void OnTriggerEnter(Collider other) {
 
                 if (other.gameObject.name == "Player") {
                         Instantiate (prefabList [prefabIndex], SpawnPosition.transform.position, SpawnPosition.transform.rotation);
 
                 }
 
         }
 
 
 
     
 
 }


The errors read: "RandomRangeInt can only be called from the main thread"

There's probably stuff in there I don't need but I'm afraid to delete any of it just in case.

Any help appreciated, let me know if more information is required. Thanks!

Comment
Add comment · Show 8
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 Dblfstr · Mar 13, 2014 at 03:47 PM 0
Share

Remove "UnityEngine" from UnityEngine.Random.Range (0, 13);

Just use Random.Range(0,13);

Then, put Random.Range(0,13) in your trigger function.

 void OnTriggerEnter(Collider other) {
 int prefabIndex = Random.Range (0, 13);
avatar image Tom_1132 · Mar 13, 2014 at 06:31 PM 0
Share

Thank you both for the replies.

I have attempted both suggestions but I'm getting the same error upon entering 'Play $$anonymous$$ode'.

Hopefully the new code and screenshot below can help identify the problem: The trigger on the left should instantiate the random prefab at the transform gizmo's current posiiton.

Been at this most of the day and I still can't figure this out :S

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 
 public class StartingAreaTileSpawner : $$anonymous$$onoBehaviour {
     
     public GameObject SpawnPosition = null;
     public int prefabIndex = Random.Range (0, 13);
     
     List<GameObject> prefabList = new List<GameObject>();
     public GameObject Prefab1;
     public GameObject Prefab2;
     public GameObject Prefab3;
     public GameObject Prefab4;
     public GameObject Prefab5;
     public GameObject Prefab6;
     public GameObject Prefab7;
     public GameObject Prefab8;
     public GameObject Prefab9;
     public GameObject Prefab10;
     public GameObject Prefab11;
     public GameObject Prefab12;
     public GameObject Prefab13;
     
     
     // Use this for initialization
     void Start () {
         
 
 
         
     }
     
     
     
     
     // Update is called once per frame
     void Update () {
         
         
     }
     
     void OnTriggerEnter(Collider other) {
         
         if (other.gameObject.name == "Player") {
             Instantiate (prefabList [Random.Range (0, 13)], SpawnPosition.transform.position, SpawnPosition.transform.rotation);
             
         }
         
     }
     
     
     
     
     
 }


alt text

screen1.png (322.5 kB)
avatar image Jeremy2300 · Mar 13, 2014 at 06:51 PM 0
Share

On line 8 you have:

 public int prefabIndex = Random.Range (0, 13);

try doing:

 public int prefabIndex;

Ins$$anonymous$$d (if you even need this variable anymore). And make sure you do the Random.Range( 0, 13 ) inside of a function.

avatar image Tom_1132 · Mar 13, 2014 at 08:18 PM 0
Share

The errors have been eli$$anonymous$$ated, but the triggers just aren't working for whatever reason.

Again, I'm a complete beginner when it comes to program$$anonymous$$g - I'm usually doing the art, but is this not already a function?

 void OnTriggerEnter(Collider other) {
         
         int prefabIndex = Random.Range(0,13);
         
         if (other.gameObject.name == "Player") 
         
         {
             Instantiate (prefabList [prefabIndex], SpawnPosition.transform.position, SpawnPosition.transform.rotation);
             
         }

I read that OnTriggerEnter2D might be required, but then that throws "must be of type Collider" errors.

Thanks for the reply.

avatar image Jeremy2300 · Mar 13, 2014 at 10:15 PM 0
Share

I haven't done too much (if anything) with the new 2D stuff in Unity. So hopefully I'm not way off on any of this.

So a few questions that come to $$anonymous$$d (again, no real background with the 2D stuff):

What are you using as on your player? (CharacterController, RigidBody, RigidBody2D, etc)

If you use OnTriggerEnter2D, did you use ( Collider2D other ), or keep the ( Collider other )?

I noticed in the screenshot that the transform of the trigger is at Z = -10. Does the Z position in the 2D space matter for collisions?

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Jeremy2300 · Mar 13, 2014 at 03:07 PM

You should be able to add all your Prefabs to your 'prefabList' in the Inspector, so I'd probably remove all of that.

You're main problem is that you're creating your prefabIndex valriable inside of Start and as such it isn't accessible outside of this function.

You could declare prefabIndex outside (along with your List and GameObject declarations) as a public of private variable.

Or use this line when instantiating:

 Instantiate( prefabList[ Random.Range( 0, 13 ) ], SpawnPosition.transform.position, SpawnPosition.transform.rotation );

There are a lot of ways to go about this, but those are my suggestions.

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

Follow this Question

Answers Answers and Comments

22 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

Related Questions

BoxCollider2D failling verification when Instantiate (Fixed, Cause: big derp) 1 Answer

What to set to make a Instantiated prefab a collider to trigger and event? 1 Answer

Collision Damage, Hit points and Instantiate 1 Answer

Player clone not instantiating at the same spot 1 Answer

[SOLVED] Destroy instantiated object prefab on collision 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