- Home /
Instantiate an Object on Another Object
Hello everyone,
I've an object (which I've attached a prefab to) that moves forward automatically on a straight path. I'm trying to instantiate an object (that I've attached another prefab to) onto the object that moves forward automatically.
Here's my script:
using UnityEngine;
using System.Collections;
public class InstantiateBulls : MonoBehaviour {
public Transform prefab;
public new Vector3 spawnPoint;
void Start () {
spawnPoint = transform.position;
}
// Update is called once per frame
void OnTriggerEnter (Collider other) {
if(other.gameObject.CompareTag("Trigger01"))
Instantiate(prefab, spawnPoint, Quaternion.identity);
Debug.Log ("We've Touched Inappropriately");
}
}
Nothing happens in the scene. I've empty game objects in the scene. I've checked the Is Trigger box on the empty game objects.. I've put a tag on them that says 'Trigger01'
I've been stuck with this problem for almost three weeks now. I've been experimenting with this and, I can get an object to instantiate at one spot, but I can't get an object to instantiate on an object. I would appreciate help.
Thank you for your time and have a nice day.
What's it colliding with? currently it will instantiate whenever it enters a trigger box attached to a gameObject with tag 'Trigger01'. Does the debug message get printed? if not then your object isn't colliding with anything.
The moving object is colliding with empty game objects. I've checked the Is Trigger box on the empty game objects. I've put a tag on them that says 'Trigger01'
The debug message doesn't get printed.
Answer by aldonaletto · Nov 07, 2012 at 10:05 PM
spawnPoint is defined at Start, and doesn't change after this. You should use the object position instead:
...
void OnTriggerEnter (Collider other) {
if(other.gameObject.CompareTag("Trigger01")){
Instantiate(prefab, transform.position, Quaternion.identity);
Debug.Log("Object created");
}
}
NOTES:
1- The script above must be attached to the moving object, not to the triggers;
2- The moving object must have a collider;
3- Either the moving object or the triggers must have a rigidbody component to make the triggers work - use a kinematic rigidbody (set Is Kinematic) in order to avoid weird physics reactions;
Answer by DaveA · Nov 07, 2012 at 10:06 PM
If by 'on an object' you mean parented to the object, then you need to set the transform.parent to the transform of the object it will be 'on'. Plus you might want to get the point at which collision occurred and use that as the position to Instantiate it
I just noticed that when I play the scene the Prefab that's attached to the script switches to another prefab. For example, before I play the scene I attach a prefab called 'BullSpawnNew' to the script. When I play the scene the prefab on the script switches from 'BullSpawnNew' to a prefab called 'PackOfBulls'
When I stop the scene, the prefab called 'PackOfBulls' is now on the script. Why is that?
$$anonymous$$y guess is you have a var (not PRIVATE) var, and you set it one way then the other. It will 'stick' with the first one, because it's now something you change in the Inspector. If you are not relying on the Inspector, make it private.
Your answer

Follow this Question
Related Questions
Instatiating a prefab in a random position 0 Answers
How can I get the x position for the left(and right) of the screen? 2 Answers
OnTriggerEnter not firing if I instantiate a GameObject completely inside another's trigger 3 Answers
Make object move in a direction depending on where it spawns? (C#) 1 Answer
Help making a damage number system 1 Answer