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!

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;
			}
		}
	}
}