- Home /
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!
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);
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);
}
}
}
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.
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.
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?
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.
Your answer
Follow this Question
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