So i have multiple duplicates of the same child (same name) under different parents in the hierarchy. I thought I was having coding issues but apon debug I found that my object would swap parents at random. After changing the name slightly this issue would go away. but now i’m faced with a issue in the code where I need to have them all be the same name. This is a unity bug???
I guarantee this is not a Unity bug. The problem is very likely to be caused by your code. My guess is that your using GameObject.Find, which you should usually try and avoid using, as it causes problems like this to be more difficult to debug. Are you setting the names of the objects anywhere in your code?
I will look over my code again, but I am not setting any names anywhere. But i do use Gameobject.Find alot. do you have a better suggestion for getting components from grandchildren?
Yes, the best way to get a component from an object is to assign it in the inspector. You should definitely avoid GameObject.Find, especially in the Update function, because it can be a very expensive call.
If you need to get the child of an object, you can simply use Transform.GetChild, and if you want a component from that child, you can use GetComponent. Though this can all also be done through the inspector if your object is not instantiated at runtime.
Let me know if you don’t understand what I mean.
I think i understand. can I make a public renderer slot in the inspector under my empty root object and drag the grandchild into that slot? This is a part of the code for my elevator buttons. the reason i have it grab it like this was because there are so many copys of the exact controller just under different root parent that it needs to grab the renderer’s from whatever root it gets called on. I would like to find an easier way to do it. I’m C# novice…
changed it so i assign the E1BBB etc… in the inspector but seems to have issues … play with it more tomorrow.
Yes, you can drag ANY component into the inspector if the variable is the same component type. Since you seem to have quite alot of Renderer objects, you may want to store them in a list, or array, and assign in the inspector. If you don’t need to be able to distinguish between the different objects, then you can store them in an array like this:
public Renderer[] elevatorRenderers;
Then in your script when you do your colour switching:
for (int i = 0; i < elevatorRenderers.Length; i++) {
Renderer cur = elevatorRenderers[i];
// Do whatever you want with this renderer ^^^
}
But if you want to be able to distinguish between them, then you can declare seperate variables like this:
public Renderer E1BBB;
public Renderer E2BBB;
public Renderer E1BB;
public Renderer E2BB;
// etc...
If you really don’t want to assign all of them in the inspector, then yes you can use Transform.Find, BUT in the Start function only. Please avoid using it in Update. Both Transform.Find, and GetComponent shouldn’t be used in Update, as doing so is very bad practice, and in almost all cases can be avoided by only calling it in Start and storing the reference into a variable.
If there is more than one elevator, then this would all be a bit more difficult. I’d say the best way to go about this would be to create a class, and give it to each elevator, then store all those renderers in that script, and access it from your player.