0

So I have a sort of item system in my game and it opens a savefile to see what items the player has at the time, when it loads it enables and disables objects based on the savefile information. Which I thought ahead and know that gameobject.find doesn't work on inactive objects so I have a script I found online to find objects by their transform to access them regardless

public static GameObject FindObjectByName(string name, int layer)
    {
        Transform[] objs = Resources.FindObjectsOfTypeAll<Transform>();
        for (int i = 0; i < objs.Length; i++)
        {
            if (objs[i].hideFlags == HideFlags.None)
            {
                if (objs[i].name == name && objs[i].gameObject.layer == layer)
                {
                    return objs[i].gameObject;
                }
            }
        }
        return null;
    }

Specifically I am trying to get an object named "shells" to be activated through this find object script, and it's just choosing not to work on this specific object. Why do I say that? Because there's three other items in the game that I use in the same way and they work perfectly fine. Which are all prefabs placed in the scene.

What I am doing is this.

RealFindObject.FindObjectByName("Shells").SetActive(true);

Both activeSelf and activeInHierarchy are lying about the object being active. Because it doesn't activate, literally nothing else is referencing this gameobject.

Even directly referencing the object by dragging it into the script as a gameobject isn't working, unless I rename the gameobject. I've found that for whatever reason, it doesn't matter what the object is, if an object is named "shells" the setactive function just chooses not to work at all. If I rename the object to "ammo" it suddenly works perfectly, so for some reason unity just doesn't like the word "shells" I guess.

This has happened to me before, I had an object called remote and unity was reacting similarly and making the object completely broken just because it was named "remote"

It doesn't matter if the object is a child or not unity just refuses to work correctly, I tried the deprecated method of setactiverecursively and it said "prefabs cannot be setactive" or something, but it's literally lying because the object isn't even a prefab. Which like I said above the other three objects are prefabs and using the scripts above works perfectly fine on them, so the error is straight up lying in two ways.

I have been stuck on this for the past two weeks and the internet has been completely useless for just searching stuff because apparently I'm the only one with this problem and it doesn't exist. So the solution I have is just to not name anything "shells" but I at least just want to know why unity is purposely being stupid about this.

5
  • if it works for everything but an object named Shells, report it as a bug to unity
    – BugFinder
    Commented Feb 3 at 10:27
  • I tried reporting it as a bug but it didn't work, the reported loaded to 62 percent after four hours and then closed saying "failed to install package". So the bug reporter is bugged I guess.
    – Bryan
    Commented Feb 3 at 20:29
  • Well if it is truly a bug you should be able to recreate it in a small new project and that would upload - the bugreporter is bad about big files as it zips up the whole folder, which will include your git, and all the junkable folders in your project.
    – BugFinder
    Commented Feb 3 at 20:31
  • Resources.FindObjectsOfTypeAll returns internal objects as well. Have you considered there is an internal object with this name, and that is the one being found first? Have you debugged what object it does find? Have you confirmed it is the same object you expect? The function you are using doesn't handle duplicate named objects at all. If you have 2 objects with the same name, this will only ever find one of them. Thats a very big concern. Its not unitys fault, they explicitly state in the documentation how it works, and those remarks were disguarded. Commented Feb 5 at 20:30
  • Unity gave you a clue that it is finding a different object than what you expect, a prefab named shells that is on the same layer. I tried the deprecated method of setactiverecursively and it said "prefabs cannot be setactive" or something. You probably have a prefab named shells. Commented Feb 5 at 20:42

0