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
2
Question by latsushi · Nov 07, 2012 at 09:44 PM · instantiatepositionontriggerenter

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.

Comment
Add comment · Show 2
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 Scribe · Nov 07, 2012 at 09:53 PM 0
Share

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.

avatar image latsushi · Nov 07, 2012 at 10:04 PM 0
Share

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.

2 Replies

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

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;

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
avatar image
0

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

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 latsushi · Nov 07, 2012 at 10:22 PM 0
Share

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?

avatar image DaveA · Nov 08, 2012 at 11:21 PM 0
Share

$$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

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

13 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

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


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