- Home /
How to hit differents gameobjects & enable the same gameobject (2d raycast)
Hello People. I write this script for hit a 2d collider and activate a specific gameobject and disable it when I hit another collider:
public GameObject NombreObjeto;
public void Awake()
{
NombreObjeto.SetActive(false);
transform.GetChild(0).gameObject.SetActive(false);
}
void Update()
{
if (Input.GetMouseButtonDown(0))
{
if (EventSystem.current.IsPointerOverGameObject())
{
Debug.Log("is over UI");
return;
}
else
{
Collidershow();
}
}
else
{
return;
}
}
private void Collidershow()
{
Vector2 origin = new Vector2(Camera.main.ScreenToWorldPoint(Input.mousePosition).x,
Camera.main.ScreenToWorldPoint(Input.mousePosition).y);
RaycastHit2D hit = Physics2D.Raycast(origin, Vector2.zero, 0f);
if (hit)
{
Debug.Log(hit.transform.name);
if (hit.collider.gameObject.name == gameObject.name)
{
NombreObjeto.SetActive(true);
transform.GetChild(0).gameObject.SetActive(true);
}
else
{
NombreObjeto.SetActive(false);
transform.GetChild(0).gameObject.SetActive(false);
}
}
}
works fine if the gameobject to enable "NombreObjeto" is different from the one enabled with the previous collider hit. but if differents colliders have the same "NombreObjeto" as mark/reference they don't get enabled/disabled.
So my question is how I can mark the same gameobject to enable from differents colliders?. PD: I want only one "NombreObjeto" enabled at once
PD2: this script is attached to each gameobject with a collider
Can give me some guidance?, thanks.
Sorry if the post its not clear, english is not my native language.
I think I can help you but I need you to confirm if I am right so
You have object A and you want to disable it when you hit it and re-enable it when you hit other object.
OR
You want to disable object A when you hit it and re-enable it when you hit object B and disable object B.
OR
If I have the wrong idea then tell me or if you want to talk we can on discord.
1- in the start object A is disabled. when I hit a specific collider A need to be enabled; later when I hit another collider I need A (previously enabled) get disabled, and enable the next gameobject called B.
this sequence works fine, the problem is this sequence:
If I hit a collider and this activate A, and later I hit another collider and try to activate A again, not works, "A" keeps disabled from the second collider.
so the Idea is if the two colliders have the same reference (gameobject A), keep it enabled.
and if one collider have reference gameobject B, so disable A, and enable B. I think my explanation is still confuse, maybe I can make a gif about what I talking about
So I think I understand what you mean if you want send a gif or we can talk on discord because you can send video if yes tell me.