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 dbz987 · Nov 05, 2012 at 08:51 PM ·

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++;
 //}
                 
             
     //}
 //}
 
         }
     
 
Comment
Add comment
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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

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);
 }
 
 }
      
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 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);
 }
 
 }
      
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

11 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

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


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