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 Spades_Neil · Oct 22, 2014 at 04:16 PM · prefabtriggermine

Communicating between objects in the same prefab?

My question is probably confusing because I don't know the right words, but here's what I want to do...

So I have a tripwire landmine I'm trying to place in my level. When the player hits the tripwire which is also a triggerbox, the wire destroys its self. That's what I have so far. The current script is attached to the tripwire.

 function OnTriggerEnter(col:Collider)
 {
     if(col.gameObject.tag == "Player")
     {
         Debug.Log("Trap tripped!");
 
         // Tell the warhead to jump and go off.
     
     
         // Make the tripwire disappear.
         Destroy(this.gameObject);
     }
 }



Now what I can't do is get this script to tell the warhead to do its thing, which is to jump up into the air and explode. I believe I will need a script for the warhead too but I have to know how to set off the warhead in the same prefab and not every single warhead on the damn map.

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 Spades_Neil · Oct 22, 2014 at 05:28 PM 0
Share

Almost helped. I'm a novice at this sort of thing.

I do not have a Warhead class. Let me just grab the whole script as it currently stands.

 #pragma strict
 
 function Start () {
 
 }
 
 function Update () {
 
 }
 
 function OnTriggerEnter(col:Collider)
 {
     if(col.gameObject.tag == "Player")
     {
         Debug.Log("Trap tripped!");
 
         // Tell the warhead to jump and go off.
     
         // $$anonymous$$ake the tripwire disappear.
         Destroy(this.gameObject);
     }
 }
 
 function triggerBomb()
 {
     // $$anonymous$$ake this jump up into the air. Something something rigidbody.velocity(0,10,0);
     // Brief time delay
     // Instantiate explosion
 }

So imagine the following: A three part prefab consisting of the warhead, a tripwire attached to the warhead, and a stake to secure the tripwire just for looks. All of these are under one GameObject called "Land$$anonymous$$e Prefab".

So I guess in order for this script to work I need to make the warhead a class. I do not know how to do this. Bare with me, I am gooing to have many noob questions.

avatar image Baste · Oct 22, 2014 at 06:24 PM 0
Share

I assumed you actually had a Warhead class because that's what "Communicating between objects in the same prefab" sounds like. To do exactly what your comments are saying, you just do:

 function OnTriggerEnter(col:Collider)
 {
     if(col.gameObject.tag == "Player")
     {
         Debug.Log("Trap tripped!");
 
         // Tell the warhead to jump and go off.
         triggerBomb();
     }
 }
  
 function triggerBomb()
 {
      // $$anonymous$$ake this jump up into the air. Something something rigidbody.velocity(0,10,0);
     rigidbody.velocity = new Vector3(0,10,0)
 
     // Brief time delay
     yield return new WaitForSeconds(brief_time); //replace with the number of seconds you want
 
     // Instantiate explosion
     Instantiate(exposionPrefab);
     
     // $$anonymous$$ake the tripwire disappear. You have to do that HERE, as the stuff after the time delay wouldn't happen if this game object was destroyed.
     Destroy(gameObject);
 }

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Baste · Oct 22, 2014 at 04:20 PM

You use the GetComponent method to find other components attached to the same GameObject.

So assuming that the warhead has the Warhead class, and the method you want to call is DoYourThing(), the code is:

 function OnTriggerEnter(col:Collider)
 {
     if(col.gameObject.tag == "Player")
     {
         Debug.Log("Trap tripped!");
 
         // Tell the warhead to jump and go off.
         var warHead : WarHead;
         warHead = gameObject.GetComponent(WarHead);
         warHead.DoYourThing();

         // Make the tripwire disappear.
         Destroy(this.gameObject);
     }
 }


Hope that helps!

Comment
Add comment · Show 1 · 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 Spades_Neil · Oct 22, 2014 at 05:31 PM 0
Share

I'm a boob and can't seem to type properly on this forum. I commented my reply on the wrong answer. Check the comments above for my response.

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

it is not possible to invoke an expression of type int 2 Answers

Spawn a prefab 1 Answer

Instantiated Prefab's script doesn't work 2 Answers

Two Instances of same Prefab dealing damage to each other. 0 Answers

Prefab Transform variable missing. 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