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 AusAndrew19 · Feb 24, 2013 at 02:06 PM · javascriptjava

How to disable "GameObject" Then Enable After Animation.

I'm adding a melee script to my FPS. so if i pres V it stabs them.

The Animation Will Be attached to a gameobject named "Quickstab" I want the mesh or mesh renderer to be invisible "Disabled" untill i press "V" and then once the animation is finished i want the object to then be disabled again. Does someone know how to achieve this? Prefer in Javascript since i dont know cs. 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 theundergod · Feb 24, 2013 at 03:08 PM

One way to work through something like this (in any programming challenge actually) is think about every step that needs to occur:

  1. You need to have a GameObject assigned for the QuickStab

  2. You need to initially disable the renderers on it

  3. Wait for the input of the "V" key

  4. Once pressed enable the renderers

  5. Play the animation

  6. Check to see if the animation is complete

  7. Disable the renderers

So one way to do it would be to create a script specifically for this object "QuickStab" object and place it on the object:

 Renderer[] renderers;
 string stabAnimation = "StabAnimation";
     
 void Start()
 {
     //get the renderers and store for later use
     renderers = GetComponentsInChildren<Renderer>();
     
     //turn off renderers initially
     foreach(Renderer r in renderers)
     {
         r.enabled = false;
     }
 }
 
 void Update()
 {
     //if V is pressed and we are not playing the animation
     if(Input.GetKeyDown(KeyCode.V) && !animation.IsPlaying(stabAnimation))
     {
         //turn on renderers and start playing the animation
         foreach(Renderer r in renderers)
         {
             r.enabled = true;
         }
         
         //play the animation
         animation.Play(stabAnimation);
     }
     //if we are playing the animation
     else if (animation[stabAnimation].time <= animation[stabAnimation].length)
     {
         //if we are playing and not at the end of the animation
         if(animation.IsPlaying(stabAnimation))
         {
             //do something like check for raycast hits
         }
         //we are at the end of the animation
         else
         {
             //ensure animation stops
             animation.Stop(stabAnimation);
             
             //disable renderers
             foreach(Renderer r in renderers)
             {
                 r.enabled = false;
             }
         }
     }
 }

This is just one of many ways to accomplish something like this. Sorry it's in C#. I'm a C# guy.

Here's my attempt to put it in JavaScript:

 var renderers : Renderer[];
 var stabAnimation : string = "StabAnimation";
     
 function Start()
 {
     //get the renderers and store for later use
     renderers = GetComponentsInChildren(Renderer);
     
     //turn off renderers initially
     for(var x : int = 0; x < renderers.length; x++)
     {
         r.enabled = false;
     }
 }
 
 function Update()
 {
     //if V is pressed and we are not playing the animation
     if(Input.GetKeyDown(KeyCode.V) && !animation.IsPlaying(stabAnimation))
     {
         //turn on renderers and start playing the animation
         for(var x : int = 0; x < renderers.length; x++)
         {
             r.enabled = true;
         }
         
         //play the animation
         animation.Play(stabAnimation);
     }
     //if we are playing the animation
     else if (animation[stabAnimation].time <= animation[stabAnimation].length)
     {
         //if we are playing and not at the end of the animation
         if(animation.IsPlaying(stabAnimation))
         {
             //do something like check for raycast hits
         }
         //we are at the end of the animation
         else
         {
             //ensure animation stops
             animation.Stop(stabAnimation);
             
             //disable renderers
             for(var x : int = 0; x < renderers.length; x++)
             {
                 r.enabled = false;
             }
         }
     }
 }
Comment
Add comment · Show 11 · 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 AusAndrew19 · Feb 24, 2013 at 04:52 PM 0
Share

Firstly Thank you for taking the time to reply in such detail really appreciate it. C# Script... 4 errors. line 1,2,4,16

 A namespace can only contain types and namespace declarations


and the JS Version Line 2 var stabAnimation : string = "StabAnimation"; unknown format String

Not too sure about the C# since i have no idea about that code..

avatar image theundergod · Feb 24, 2013 at 05:07 PM 0
Share

As far as the C# code is concerned it will only work if you put the above code in a C# script within the class declaration such as

 using UnityEngine;
 using System.Collections;
 
 public class StabObject : $$anonymous$$onoBehaviour
 {
      //Paste the code here
 }

For the Javascript I'm not sure. Does javascript accept double quotes (") or do you have to use single quotes (')?

avatar image AusAndrew19 · Feb 24, 2013 at 05:14 PM 0
Share

C# Isnt Getting Those Errors Now.. But There getting this.

if(Input.GetButtonDown($$anonymous$$eyCode.V) && !animation.IsPlaying(stabAnimation))

Argument #1' cannot convert UnityEngine.$$anonymous$$eyCode' expression to type string' The best overloaded method match for UnityEngine.Input.GetButtonDown(string)' has some invalid arguments

same line.

JS uses both.. But the error is i dont think javascript uses "String" The name 'string' does not denote a valid type ('not found'). Did you mean 'Boo.Lang.Compiler.IO.StringInput'?

avatar image theundergod · Feb 24, 2013 at 05:14 PM 0
Share

Oh one more thing with the Javascript: do you even declare string types in Javascript? Since it's loosely typed maybe it should just be:

 var stabAnimation = "StabAnimation";

Like I said I'm a C# guy so sorry I'm not too much help with the Javascript.

avatar image theundergod · Feb 24, 2013 at 05:17 PM 0
Share

Oops not GetButtonDown shoudl be Get$$anonymous$$eyDown. I will update the code to reflect the correction.

Show more comments

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

10 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

Related Questions

How to display GUI in sequence in trigger 3 Answers

change water color through script when an action occurs 3 Answers

Convert this line of javascript to C# (easy) 1 Answer

oescape script name? 1 Answer

Is it the same javascript 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