Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 Sun_Glasses_Guy · Jul 14, 2011 at 02:55 AM · javascriptgetcomponentweaponexplosionremote

Variable Access Problem (Javascript)

/I USE JAVASCRIPT\

Hey, I'm working on a little practice project. I'm trying to make a remote explosive, basically a C4 charge. I have gotten the C4 prefab to instantiate, but I need a way to tell the C4 charge that I spawned to destroy itself and instantiate an explosion in it's place.

I've tried a GetComponent but the whole concept is confusing and it just seems like people are just pretending to know what they're talking about because half of what they type is usually copied from the Unity script references, and those are a little vague and incomprehensible on the subject.

I've tried making a function on the C4's trigger that adds 1 to a variable (which is at 0) and then checking if the variable is over 0 on the C4 Charge then trying to destroy the Charge while instantiating an explosion. But I just can't figure out how to get the variable.

I have everything for the C4 working(animations, instantiating the charge) except for the part on how to access the variable and trigger the explosion.

Please help, I can't find the answers on Google and I don't completely understand the Unity Script References for the GetComponent function.

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

3 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Chris D · Jul 14, 2011 at 03:43 AM

On your player:

 var C4 : GameObject;

 function Update(){
     if (Input.GetButtonDown("Fire1")){
         C4.SendMessage("Explode");
     }
 }

On your C4:

 function Explode(){
 //instantiate your explosion
 //destroy your C4
 }
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 Sun_Glasses_Guy · Jul 16, 2011 at 05:27 PM 0
Share

It looked promising, but it didn't work. It says that "Send$$anonymous$$essage("Explode") has no receiver!"

avatar image aldonaletto · Jul 16, 2011 at 06:04 PM 0
Share

Try to add DontRequireReceiver to Send$$anonymous$$essage:

C4.Send$$anonymous$$essage("Explode",null,Send$$anonymous$$essageOptions.DontRequireReceiver);
avatar image
0

Answer by aldonaletto · Jul 14, 2011 at 03:44 AM

The trigger must be part of the C4 charge, and you can make the C4 explode in its script's OnTriggerEnter. Attach this script to the C4 prefab, then drag the explosion prefab to the variable explosion in the Inspector:

 var explosion: GameObject; // drag the explosion here
 
 function OnTriggerEnter(col:Collider){
 
   if (col.tag == "Player"){ // only the Player explodes it
     //instantiate the explosion
     Instantiate(explosion, transform.position, Quaternion.identity);
     Destroy(gameObject); // suicide this C4 object
   }
 }

I'm supposing that:
- the trigger is part of the C4 prefab;
- the player has the tag "Player";

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 Sun_Glasses_Guy · Jul 16, 2011 at 05:24 PM 0
Share

I didn't mean a ColliderTrigger! I meant the little button/plunger that sets off the explosive.

avatar image aldonaletto · Jul 16, 2011 at 06:21 PM 0
Share

And what's this button? Part of the C4 explosive or another independent object? If it's another object (not childed to C4) it's better to use @ChrisD's alternative: Send$$anonymous$$essage("Explode") actually calls the function Explode in all objects which have such a function. But it may be disastrous if you have several C4 in your scene. If this is the case, the better alternative is to have a GameObject variable in the button script, and drag the C4 object to it - this way each button knows which C4 to detonate.

avatar image
0

Answer by AdamOwen · Jul 16, 2011 at 07:02 PM

If you have everything else set up and just need to tell the C4 script that it needs to explode then GetComponent will work fine.

On your button / plunger / trigger script:

 var buttonPressed : boolean;

 function Update() {
     // I'm using pressing the spacebar as the trigger as an example
     if (Input.GetKeyDown("space")) {
         buttonPressed = true;
     }

     if (buttonPressed) {
         // We're saying find the GameObject called c4 and get the attached script C4Script
         // Quotes aren't necessary around C4Script as it's a type
         var c4Script : C4Script = gameObject.Find("c4").GetComponent(C4Script);

         // Now you can access all public variables and functions contained in that script
         c4Script.explodeC4(); // This calls your explode function on the C4

         buttonPressed = false; // Set this to false to stop explodeC4 being constantly called
     }
 }

Then on your c4 script you can create your explode function:

 function explodeC4() {
     // Instantiate the explosion effect and destroy the GameObject
 }
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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Help With Remote Explosive 1 Answer

How could you access a script of varying name? 5 Answers

distance script not working!!! 1 Answer

Cycling through enum with key press 1 Answer

create GUI.Label and then access to it.. 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