- Home /
how to enable and disable script.
This is a baseball game. I have a script called IsGrounded with a boolean.It gets triggered to false which makes another script called fielding become false.This is showing both to be enabled = false,which is what i want.Now fielding i thought would be inactive without the script being checked as true,but it is still active even though it is checked to false. I have read many other things about this in unity answers which says the scripts are still active unless i enable or disable in update,Late update or maybe Start function.how can i change my code for this i dont have update in my fielding script.here my codes
IsGrounded code
var IsGrounded = false;
function Update()
{
IsGrounded = false;
}
function OnCollisionEnter(other:Collision)
{
if(other.collider.name == ("Terrain"))
IsGrounded = true;
if(IsGrounded == true)
{
gameObject.Find("P_SS").GetComponent("Fielding").enabled = false;
Debug.Log("Grounded");
}
}
fielding code
function OnCollisionEnter(other:Collision)
{
if(other.collider.name == ("BaseBall(Clone)"))//&& IsGrounded == false)
{
Destroy(other.gameObject);
audio.Play();
}
}
Answer by Simeon · Feb 25, 2014 at 01:10 AM
First of all, it will be a good idea to use tags instead of names, and i think the code will go something like this:
var IsGrounded = false;
function Update()
{
IsGrounded = false;
}
function OnCollisionEnter(other:Collision)
{
if(other.collider.tag == ("Terrain")){
IsGrounded = true;
gameObject.Find("P_SS").GetComponent("Fielding").enabled = false;
Debug.Log("Grounded");
}
}
function OnCollisionEnter(other:Collision)
{
if(other.collider.tag == "BaseBall") //&& IsGrounded == false)
{
Destroy(other.gameObject);
audio.Play();
}
}
and another thing, if you want to destroy objects immediately use:
DestroyImmediate(obj: Object, allowDestroyingAssets: bool = false):
OnCollisionEnter already has a definition.Cant do that.I will try trigger on the second part.I cant use a trigger here it ball will go through the terrain.I put both parts of code in the same OnCollisionEnter.It has the same outcome as my original code.Shows enabled box for terrain as off but yet i can still get collision from fielder.I need fielder code to be true from fielder only when the ball is airborne.Grounded i need fielder code to be shut off.Not sure where to go with this.Basically the fielder is as if he is catching the ball in the air audio.Play(); for an out always, even though the ball has hit the ground first.I need a grounder to be treated as a grounder. ty Dimwood