- Home /
how do I Instantiate a game object on a trigger
Right now I am instantiating a bull to create other bulls to chase the player. But it doesn't work for me. I created a prefab and attached to a game object but it still doesn't work. Do i need Transfrom.position and rotation to get this right
Here is the script that im using
Note: i don't have a lot of scripting skills
using UnityEngine;
using System.Collections;
public class BullAppear : MonoBehaviour {
public Transform prefab;
// Update is called once per frame
void OnTriggerEnter (Collider other) {
//if(Input.GetKeyDown(KeyCode.L))
if(other.gameObject.CompareTag("Trigger01"))
Instantiate(prefab);
Debug.Log ("We've Touched Inappropriately");
}
//OnTriggerEnter(Collider other){
//void SpawnBull(){
//int i = 0;
//while (i < 10){
//Instantiate(prefab, new Vector3(i * 2.0f, 0, 0), Quaternion.identity);
//i++;
//}
//}
//}
}
Answer by sparkzbarca · Nov 06, 2012 at 05:10 AM
well instantinate does copy the transform of the prefab so yes you would want to first set the prefabs transform to something.
Are you sure its not working first (by that i mean check the hierarchy in unity, does it show an object being added to the scene even if you cant see it?)
according to your code an object tagged trigger01 is supposed to walk into...something, whatever this script is attached to.
the spawn bull script is spawning at the edge of the map basically. the vector 3 should not be what it is.
First off your middle value Y thats the height. Your spawning the middle of the bull object at height 0. Thats the height of the ground. your putting the legs and everything below the midpoint of the bull under the terrain plane. They might even be falling through the ground. Hence them spawning in but you cant see them they should still show up in the inspector though in the hierarchy stuff on the bottom left.
what you should spawn is relative to the player or the trigger point. for example
ontriggerenter() {
//you seem to be creating a bull that then creates more of
//himself. were not going to do that, were just going to
//create 11 bulls off the start.
OnTriggerEnter(collider other)
{
//i use "this" for clarity its not needed, this basically
// means whatever object the script is attached to, its an
// actual keyword but its implict so you dont have to use it.
//in this case "this" should be the trigger object itself.
prefab.position = this.transform.position + vector3.forward();
//vector3.forward is shorthand for vector3(0,0,1);
for(int counter = 0; counter < 11; counter++)
{
// += adds whatever to the current value
//prefab.posiiton += vector3.forward() is the same
//thing as writing
//prefab.position = prefab.position + vector3.forward();
//now each bull will spawn 1 unit further and further
//deep in basically a line.
prefab.position += vector3.forward();
instantiate(prefab);
}
}
Answer by sparkzbarca · Nov 06, 2012 at 05:10 AM
well instantinate does copy the transform of the prefab so yes you would want to first set the prefabs transform to something.
Are you sure its not working first (by that i mean check the hierarchy in unity, does it show an object being added to the scene even if you cant see it?)
according to your code an object tagged trigger01 is supposed to walk into...something, whatever this script is attached to.
the spawn bull script is spawning at the edge of the map basically. the vector 3 should not be what it is.
First off your middle value Y thats the height. Your spawning the middle of the bull object at height 0. Thats the height of the ground. your putting the legs and everything below the midpoint of the bull under the terrain plane. They might even be falling through the ground. Hence them spawning in but you cant see them they should still show up in the inspector though in the hierarchy stuff on the bottom left.
what you should spawn is relative to the player or the trigger point. for example
ontriggerenter() {
//you seem to be creating a bull that then creates more of
//himself. were not going to do that, were just going to
//create 11 bulls off the start.
OnTriggerEnter(collider other)
{
//i use "this" for clarity its not needed, this basically
// means whatever object the script is attached to, its an
// actual keyword but its implict so you dont have to use it.
//in this case "this" should be the trigger object itself.
prefab.position = this.transform.position + vector3.forward();
//vector3.forward is shorthand for vector3(0,0,1);
for(int counter = 0; counter < 11; counter++)
{
// += adds whatever to the current value
//prefab.posiiton += vector3.forward() is the same
//thing as writing
//prefab.position = prefab.position + vector3.forward();
//now each bull will spawn 1 unit further and further
//deep in basically a line.
prefab.position += vector3.forward();
instantiate(prefab);
}
}
Your answer

Follow this Question
Related Questions
A node in a childnode? 1 Answer
How do i make an animation play on key press? 3 Answers
First Person RPG Logic... 0 Answers
Animation Not Playing 1 Answer
Script Help 3D touch animation 1 Answer