is there a way to access components inside a cube (example) without adding my script to him?
like, without adding my script as a component?
GameObject.Find("Cube").GetComponent<MyScript>();
without adding the script to anything in the hierarchy?
this code work?
You need to adds scripts in something in the hierarchy, otherwise they can’t run. That’s how Unity works.
–Eric
Well that can be a little misleading as well.
At least 1 script has to be attached to a GameObject, as that’s the only hook you really have to get your code up and started.
But from there, you technically can write all your code as classes that aren’t components. But honestly, the components make a lot of things easier… still though, I have a large amount of classes that aren’t components.
Except for the PostProcessScene attribute
:
http://docs.unity3d.com/ScriptReference/Callbacks.PostProcessSceneAttribute.html
Let’s you build your whole scene from code.
I was actually trying to think of an attribute that allows for auto-execute of a method.
Thing is that is a UnityEditor attribute, so it won’t work at runtime. Same goes for ‘InitializeOnLoad’, another that auto-executes, but is editor only too.
Also, any code you have would need some hook into the main thread (primarily Update and FixedUpdate). So really… you’d want at least one script to facilitate that pump.
The way it works is that it runs the method before the scene is run AND when the scene is built as well. Meaning that if you create a GameObject in there, it will be in the built product.
Well then, that’s technically adding a script to a GameObject.
Right, just you don’t have to visually drag it in the inspector. You can have a completely empty scene and create the whole scene programmatically.