Quantcast
Channel: Questions in topic: "loops"
Viewing all articles
Browse latest Browse all 184

Preventing name duplication in Hierarchy(getting selected GameObject)

$
0
0
I'm trying to write a script that would prevent user from having two gameObjects with the same name. For that I subscribed to the `EditorApplication.hierarchyWindowChanged` event. I wrote some code that loops trough all gameObjects in the scene and finds duplicates. It works great, my problem is just that it always gets the first item that is duplicated. But I am trying to get the item that was renamed to match witch another(that was selected at that time). int count1 = 0, count2 = 0; foreach (GameObject obj in UnityEngine.Object.FindObjectsOfType(typeof(GameObject))) { foreach (GameObject obj2 in UnityEngine.Object.FindObjectsOfType(typeof(GameObject))) { if (count1 != count2 && obj.name == obj2.name) { obj.name += "0"; } count2++; } count1++; count2 = 0; } To prevent duplication I am adding "0" to a duplicated object, but of course, not always to the selected one but always to the first in list.

Viewing all articles
Browse latest Browse all 184

Latest Images

Trending Articles



Latest Images

<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>