Question by
molestium · Aug 16, 2020 at 12:19 PM ·
destroy objectload scene
How remove double name objects in trigger?
I'm trying to make a scene changing, but if the objects inside the trigger move to previous scene, Destroy() function ignores my code and randomly removes objects which should have saved.
public class DontDestroyObject : MonoBehaviour
{
private void Awake()
{
foreach (var gameObj in FindObjectsOfType(typeof(GameObject)) as GameObject[])
{
if (gameObj.name == name)
{
if (gameObj != gameObject)
{
Destroy(gameObj.gameObject);
}
}
}
for (int i = 0; i < transform.childCount; i++)
{
foreach (var gameObj in FindObjectsOfType(typeof(GameObject)) as GameObject[])
{
if (gameObj.name == transform.GetChild(i).name)
{
if (gameObj != transform.GetChild(i))
{
Destroy(gameObj.gameObject);
if (transform.GetChild(i).gameObject.GetComponent<PlayerController>() != null)
{
transform.GetChild(i).gameObject.GetComponent<PlayerController>().enabled=true; }
}
}
}
} ;
DontDestroyOnLoad(gameObject);
}
private void Update()
{
}
private void OnTriggerEnter(Collider other)
{
other.transform.SetParent(transform);
}
private void OnTriggerExit(Collider other)
{
other.transform.SetParent(null);
SceneManager.MoveGameObjectToScene(other.gameObject, SceneManager.GetActiveScene());
}
}
Comment