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
1
Question by jothetomato · Mar 21, 2013 at 07:17 PM · instantiateevents

Can one add events to an instantiated object?

Hello community,

Just starting on unity and I was wondering if it was possible to add event listeners to objects that were instanciated:

 var bullet : GameObject;
 
 function Update () {
     if(Input.GetButtonUp('Jump')){
         bullet = GameObject.Instantiate(bullet, this.transform.position, this.transform.rotation);
         bullet.rigidbody.AddRelativeForce(Vector3(2000,0,0));
         
         // this is what I would like to do:
         bullet.on('collisionEnter', function(coll: Collider){
             Debug.Log('ok');
         });
     }
 }
 }

Or is the only way to create a script for the bullet behaviour itself?

My main interest would be to apply different listeners according to a given parameter.

Thanks

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

1 Reply

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

Answer by dubbreak · Mar 21, 2013 at 09:13 PM

Step 1. Use c# so you can define events (JS can only subscribe to event IIRC).

Step 2. Create an event in your bullet class.

 public class MuhBullet : MonoBehaviour 
 {
     public string BulletId;
     
     public event Action BulletCollided; //event using the default Action delegate
     
     public Delegate void CollisionWithDataDelegate(Collision collision, string name)
     public event CollisionWithDataDelegate BulletDataCollision; //example with custom delegate
     
     void OnCollisionEnter(Collision collision) 
     {    
         if (BulletCollided!=null) BulletCollided(); //Raise event, if subscribers
         
         if (BulletDataCollision != null) BulletDataCollision(collision, BulletId); //Raise a different one that has data, if subscribers
     }
 }


Step 3. Subscribe to event.

 bullet = GameObject.Instantiate(bullet, this.transform.position, this.transform.rotation);
 bullet.BulletId = "Bill";
 
 bullet.BulletCollided += DoSomethingOnBulletCollision;
 bullet.BulletDataCollision += DoSomethingWithData;
 
 bullet.rigidbody.AddRelativeForce(Vector3(2000,0,0));
 
 //somewhere else
 public void DoSomethingOnBulletCollision()
 {
     Debug.Log("Bullet collided!");
 }
 
 public void DoSomethingWithData(Collision collision, string name)
 {
     Debug.Log("A bullet of the following name collided: " + name);
 }

Step 4. Do cool things and be happy.

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 jothetomato · Mar 22, 2013 at 12:03 AM 0
Share

c# it is then :)

avatar image dubbreak · Mar 22, 2013 at 12:39 AM 0
Share

That's the way to go with Unity I$$anonymous$$HO. Unity's JS is their own beast that is useless outside of Unity (whereas c# is actually used, a lot ).

It's definitely worth the (tiny) learning curve of a different syntax. Unity JS has to be learned anyhow by anyone new to it (since it isn't actually javascript), so might as well learn the language that is useful outside of Unity.

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

Checking if object intersects? 1 Answer

"Destroy assets is not permitted to avoid data loss" 2 Answers

Instantiate Problem 2 Answers

Problems with Sprite.Create in 5.0.1 0 Answers

Get unity to recognize prefab in C# 2 Answers


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