The 'this' keyword in Unity3D scripts (c# script, JavaScript) -
i'm confused that:
code (cs01.cs)
the script attached simple cube object.
using unityengine; using system.collections; public class cs01 : monobehaviour { // use initialization void start () { debug.log (this); //--> cube(cs01) debug.log (this.gettype()); //--> cs01 debug.log (this.gettype() == typeof(unityengine.gameobject)); //--> false debug.log (this == gameobject); //--> false debug.log (this.name); //--> cube debug.log (gameobject.name); //--> cube this.name = "hello"; // --> gameobject's name changed 'hello' debug.log (gameobject.name); //--> hello } // update called once per frame void update () { } } 1> cs01 script object right?
doc: scripting inside unity consists of attaching custom script objects called behaviours game objects.
2> in component object, variables transform, renderer, rigidbody referenced components of gameobject component attached to. in script, this.renderer.material.color = color.red; equivalent this.gameobject.renderer.material.color = color.red. however, description of variable name in document name: name of object. tells it's name of object.
3> so, how understand code above? variable name return name of gameobject script attached to?
4> this means script object not gameobject script attached to, right?
if attach cs01 game object in scene , game object loaded, have 1 instance of each. during construction of cs01 before awake , onenable called, properties gameobject, tranform, name, etc. initialised. of them reference parent's properties if refering object.
string class special in c# or java , behaves rather struct - kind of copied. in detail: cs01.name (which inherited unity.object) game object's name. long have both same name, both variables point same place in memory. when change cs01's name, new string instance created internally , initialised text assign it. original i.e. name property of game object remain unchanged.
- yes
- most components have properties refer
gameobjectproperties, kind of shortcut - like in 2.
namedefined in base classobject.gameobject,monobehaviourboth derivedunity.object. - exactly, class writing in code
Comments
Post a Comment