0

This add mesh collider to all gameobjects also prefabs even if childs in the prefab already have colliders. The problem is that when I quit the game stop playing it's not keeping the colliders I added. How can I keep the colliders ?

using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using UnityEngine;

public class GetComponents : MonoBehaviour
{
    public Transform parent;
    public List<GameObject> allObjects = new List<GameObject>();

    private void Start()
    {
        string path = "e:/colliders.txt";
        StreamWriter writer = new StreamWriter(path, true);

        allObjects = FindObjectsOfType<GameObject>().ToList();

        Transform[] allChildren = parent.GetComponentsInChildren<Transform>();

        foreach (Transform child in allChildren)
        {
            if (child.name != "_Level" && child.name != "_Rocks" && child.name != "_Others" && child.name != "Base")
            {
                var colliders = child.GetComponents<Collider>();

                int length = colliders.Length;

                if (length == 0)
                {
                    writer.WriteLine(string.Format("{0} - No Colliders", child.name));
                    child.gameObject.AddComponent<MeshCollider>();
                }
                else
                {
                    //composes a list of the colliders types, this will print what you want e.g. "Wall1 - BoxCollider, MeshCollider"

                    string colliderTypes = string.Empty;

                    for (int i = 0; i < length; i++)
                    {
                        colliderTypes = string.Format("{0}{1}", colliderTypes, colliders[i].GetType().Name);

                        if (i != (length - 1))
                        {
                            colliderTypes = string.Format("{0}, ", colliderTypes);
                        }
                    }
                    //writer.WriteLine(string.Format("{0} - {1}", child.name, colliderTypes));
                }
            }
        }

        writer.Close();
    }
}
Daniel Lip
  • 3,867
  • 7
  • 58
  • 120
  • 2
    what are you trying to do? Changes you make during game to the objects doesn't change the game code, that would be odd. You're either looking for https://stackoverflow.com/questions/40965645/what-is-the-best-way-to-save-game-state or https://answers.unity.com/questions/43972/how-do-i-commit-changes-in-test-mode.html But maybe you were trying to do custom editor extension https://docs.unity3d.com/Manual/editor-EditorWindows.html – Adassko Oct 19 '19 at 19:00
  • Changes made to the scene in play mode do not persist after you stop it. The only way to make them persist, at least this is what I do, is to: 1) in play mode, copy everything in the scene, or the parts that were changed, 2) exit play mode, 3) paste the objects. Note that I only do this with a single object, not sure if it will work for multiple, but I don't see why not. – Vincent Bree Oct 21 '19 at 22:30

0 Answers0