I am instantiating a prefab GameObject that possesses a C# script. Within this script I need to reference the same GameObject that possesses this script. How do I do this? I will have to do this for multiple instantiations of the GameObjects. Not that it’s particularly relevant, but I am wanting to reference the GameObject so that I can change the layer value. Much thanks in advance to any help given.
From any script, on that game object, you can use ‘gameObject’ (with a small ‘g’) to reference the game object itself. So you can do:
gameObject.name = "NewName";
gameObject.layer = 2;
Note that often you will end up with other components of other game objects. Like it’s collider or its transform.
other.gameObject.name = "foo";