- Home /
How do you enable/disable colliders of specific child gameobjects? {java}
I'm running a test where a gameObject is changing sprites from a box to a skateboard to test changing colliders. So far, I've created two empty child gameobjects with colliders corresponding to the two sprites' shapes {one box collider, & one polygon collider}. What I'm trying to do is enable one collider & disable the other whenever I want to switch between the two, but none of the answers I've found so far have worked. Closest I've gotten with the fewest errors is this:
gameObject.Find("boxCol").GetComponent.BoxCollider().Enabled = true;
gameObject.Find("skakteCol").GetComponent.PolygonCollider().Enabled = false;
Any help is much appreciated
Answer by SohailBukhari · Jul 04, 2017 at 09:14 AM
you are not using correct syntax for finding gameObject. The GameObject which you are trying to find make sure is active in hierarchy then disable. For more information go to link https://docs.unity3d.com/ScriptReference/GameObject.Find.html
#pragma strict
function Start () {
var BoxCol:
BoxCollider = gameObject.Find("boxCol").gameObject.GetComponent. < BoxCollider > ();
BoxCol.enabled = false;
}
It's giving me an error message "$$anonymous$$ identifier: 'boxCol'." as well as with skateCol Is it the same syntax for the PolygonCollider2D? Here's the whole script:
var longboard : Sprite;
var testBo : Sprite;
function Start () {
var boxCol:
BoxCollider = gameObject.Find("boxCol").gameObject.GetComponent. < BoxCollider > ();
var skateCol:
PolygonCollider2D = gameObject.Find("skateCol").gameObject.GetComponent. < PolygonCollider2D > ();
}
function Update () {
if(GameObject.Find("wWolf").transform.position.x > 0){
transform.position.x = 20;
GetComponent(SpriteRenderer).sprite = longboard;
boxCol.enabled = false;
skateCol.enabled = true;
}else{
transform.position.x = -3;
GetComponent(SpriteRenderer).sprite = testBo;
boxCol.enabled = true;
skateCol.enabled = false;
}
}
Variables should be declared outside functions.
var longboard : Sprite;
var testBo: Sprite;
var boxCol: BoxCollider;
var skateCol: PolygonCollider2D;
function Start ()
{
boxCol = gameObject.Find("boxCol").GetComponent.<BoxCollider>();
skateCol = gameObject.Find("skateCol").GetComponent.<PolygonCollider2D>();
}
That got rid of the errors, thanks But is seems that both colliders are both enabled &
function Update () {
if(GameObject.Find("wWolf").transform.position.x > 0){
transform.position.x = 20;
GetComponent(SpriteRenderer).sprite = longboard;
boxCol.enabled = false;
skateCol.enabled = true;
}else{
transform.position.x = -3;
GetComponent(SpriteRenderer).sprite = testBo;
boxCol.enabled = true;
skateCol.enabled = false;
}
}
is changing the sprites & the x position, but both colliders are stuck enabled.
Hmm.... i think you pasted my code in the c# script ins$$anonymous$$d of JavaScript file.$$anonymous$$ake sure you created JavaScript script.
i pasted the link of unity documentation you can see more details from the unity panel, this is basic,try to find first at unity documentation about syntax errors, if your problem not solved then ask question.
Answer by Vollmondum · Jul 04, 2017 at 10:57 AM
You're dong everything right, just need to add a condition for a switch:
var usingSkateboard: boolean;
var switchCollider: boolean;
if(switchCollider)
{
switchCollider = false;
usingSkateboard = !usingSkateboard;
}
if(usingSkateboard)
{
gameObject.Find("boxCol").GetComponent.<BoxCollider>().enabled = true;
gameObject.Find("skakteCol").GetComponent.<PolygonCollider>().enabled = false;
}
else if(!usingSkateboard)
{
gameObject.Find("boxCol").GetComponent.<BoxCollider>().enabled = false;
gameObject.Find("skakteCol").GetComponent.<PolygonCollider>().enabled = true;
}
And then the only thing you need is to tick that switchCollider thing.
Your answer
Follow this Question
Related Questions
image not enabling 0 Answers
Disabling collsions between MeshColliders and bullets 2 Answers
Enable/Disable Game Object With GUI Button 1 Answer
Enabling loop and disabling loop 1 Answer
Enable/Disable a Static object 1 Answer