- Home /
Get all unknow objects colliding with a known object.
I have a Gameobject[] with wall objects that are to be set invisible when the player enters a trigger. How do i add objects that get in contact with the objects in the array at runtime to a list of gameobjects to also set them invisible.
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using UnityEngine;
[RequireComponent(typeof(Rigidbody))]
public class HouseTransparency : MonoBehaviour
{
public GameObject[] setInvisible = null;
public List<GameObject> objectsToInclude = null;
public int count = 0;
void OnTriggerEnter(Collider other)
{
if (other.tag == "Player")
{
count++;
foreach (GameObject obj in setInvisible)
{
obj.GetComponent<MeshRenderer>().shadowCastingMode = UnityEngine.Rendering.ShadowCastingMode.ShadowsOnly;
}
}
}
private void OnTriggerExit(Collider other)
{
count--;
if (other.tag == "Player" && count == 0)
{
foreach (GameObject obj in setInvisible)
{
obj.GetComponent<MeshRenderer>().shadowCastingMode = UnityEngine.Rendering.ShadowCastingMode.On;
}
}
}
}
Thank you.
Hie @$$anonymous$$ythran . Could you please clarify what do you want to do. Do want a list of all the objects colliding with a specific collider?
Indeed. I enter the house that has a trigger, and the whole walls and floors of the upper floors turn invisible, which are the objects that i added to the GameObject[], but the player also can drop item, and move items around. So if the new objects the player left behind are colliding with any of the objects that are set to turn invisible i want to turn these new items also invisible. Thank you @JustAbhi
Your answer