In lua, a different scripting language, you can access stuff easy using stuff like this:
local GameObject Sphere = game.Workspace.Sphere1;
or like this:
local GameObject Sphere2 = script.Parent.Parent;
how would i do this in unitty?
In lua, a different scripting language, you can access stuff easy using stuff like this:
local GameObject Sphere = game.Workspace.Sphere1;
or like this:
local GameObject Sphere2 = script.Parent.Parent;
how would i do this in unitty?
Magic, of course.
Would it be like…
this.Sphere2
to access ?
Unlike dynamically typed languages such as Lua or JavaScript, Unity doesn’t store its scene hierarchy in a form that is optimized for looking up by name. Nor did C# allow such syntax in its early days anyway, optimal or not (though it does now through dynamic objects).
If you really want to navigate the hierarchy by name, you can use functions such as Transform.Find(name), but it is not generally advised. Better to have the necessary object references on one script; these can then be set from within the editor by dragging the appropriate object to be referenced into the inspector field. This keeps the hierarchy more flexible, so that you can rearrange it and rename things without having to adjust your code every time. It also keeps things performant, by having the reference already set from the very beginning without ever having to do a lookup of any kind.
that makes a lot of sense and i didnt think about it, thanks.
P.S its time to get new glasses