• 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 ImPvEspec · Apr 09, 2013 at 05:57 AM · gameobjectdestroyyieldfireball

How can I delay the cast of a fireball as well as destroy a gameobject mid script

So I have this script launch a fireball on a 3 second cooldown, that works fine. Im trying to implement a cast time by spawning a flame object that lasts 1.5 secs then disapears as the fireball is cast. I keep getting the most retarded error ever so i assume im writing it way wrong...

 #pragma strict
 
 public var missileObject:GameObject;
 public var throwForce:float = 10;
 private var canFire : boolean = true;
 public var flameObject:GameObject;
 public var fireAnchor:GameObject;
 
 function Start () {
 
 }
 
 function Update () {
     if(Input.GetKeyDown("2") && canFire){
             var newFlame:GameObject = Instantiate(flameObject,fireAnchor.transform.position, fireAnchor.transform.rotation);
             yield WaitForSeconds (1.5)
                 Destroy newFlame:GameObject;
             var newMissile:GameObject = Instantiate(missileObject, this.transform.position, this.transform.rotation);
                 Physics.IgnoreCollision(newMissile.collider, this.transform.parent.collider);
                 newMissile.rigidbody.AddForce(throwForce * this.transform.forward, ForceMode.Impulse);
     canFire = false;
         Reload();    
     }
 }
 
 function Reload (){
     yield WaitForSeconds (3);
         canFire = true;
         
 }
 
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
Best Answer

Answer by whydoidoit · Apr 09, 2013 at 06:01 AM

You need to do:

  Destroy(newFlame);

But also you cannot yield inside an Update function - as you see with the Reload - in fact you are better off putting all of the stuff inside the if inside reload now:

  function Update () {
     if(Input.GetKeyDown("2") && canFire){
          
        FireAndReload(); 
     }
 }
  
 function FireAndReload (){
          canFire = false;

          var newFlame:GameObject = Instantiate(flameObject,fireAnchor.transform.position, fireAnchor.transform.rotation);
          yield WaitForSeconds (1.5)

          Destroy(newFlame);

          var newMissile:GameObject = Instantiate(missileObject, this.transform.position, this.transform.rotation);
          Physics.IgnoreCollision(newMissile.collider, this.transform.parent.collider);
          newMissile.rigidbody.AddForce(throwForce * this.transform.forward, ForceMode.Impulse);

          yield WaitForSeconds (3);
          canFire = true;
  
 }
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 ImPvEspec · Apr 09, 2013 at 02:45 PM 0
Share

Interesting.. Thank you very much. Guess i'll be spending my uni holidays spicing up my programming capabilities.

avatar image
0

Answer by ImPvEspec · Apr 23, 2013 at 07:36 AM

This was the final script I ended up using, still a slight issue with the instantiating of the 1st flame effect as it doesnt follow the character, though its only alive for a second so not to bad but rest of it works fine.

 #pragma strict
 
 public var missileObject:GameObject;
 public var throwForce:float = 10;
 private var canFire : boolean = true;
 public var flameObject:GameObject;
 public var fireAnchor:GameObject;
 public var fireballCast:AudioClip;
 public var fireballLaunch:AudioClip;
 
 
 
 function Start () {
 
 }
 
 function Update () {
     if(Input.GetKeyDown("2") && canFire){   //Press 2 key in game to initiate script
             
     FireandReload();    //Calls the below function
     }
 }
 
 function FireandReload (){
     var newFlame:GameObject = Instantiate(flameObject,fireAnchor.transform.position, fireAnchor.transform.rotation); //Instantiates the flame effect on the players hands
         audio.PlayOneShot (fireballCast);
             yield WaitForSeconds (1);
                 Destroy (newFlame); //Destroys flame effect
     var newMissile:GameObject = Instantiate(missileObject, this.transform.position, this.transform.rotation); //Instantiates the fireball in front of the player
         Physics.IgnoreCollision(newMissile.collider, this.transform.parent.collider); //Stops the fireball colliding with the player
             newMissile.rigidbody.AddForce(throwForce * this.transform.forward, ForceMode.Impulse); //Propels the fireball forwards
                 audio.PlayOneShot(fireballLaunch);
         canFire = false; //Script can now no longer player again
             yield WaitForSeconds (3);
         canFire = true; //After 3 second delay script is reset
         
 }
 
 
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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



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

using Contains(gameObject) to find and destroy a gameObject from a list 2 Answers

Yeild and WaitForSeconds and Instantiate 1 Answer

Check if object is destroyed on level load, if so instantiate prefab? 1 Answer

Destroy GameObject won't destroy 1 Answer

Simple Way to Remove a Single Object on Click/Touch 2 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges