unity object with gameobject code example
Example 1: gameobject in unity c#
//define "myObject"
//(asign in in inspector)
public GameObject myObject;
//runs when you hit the play button
void Start()
{
//change the name to "myObjectsNewName"
myObject.name = "myObjectsNewName";
}
//Note for Unity and C#
Example 2: unity object with gameobject
using UnityEngine;
using System.Collections;// This returns the GameObject named Hand in one of the Scenes.public class ExampleClass : MonoBehaviour
{
public GameObject hand; void Example()
{
// This returns the GameObject named Hand.
hand = GameObject.Find("Hand"); // This returns the GameObject named Hand.
// Hand must not have a parent in the Hierarchy view.
hand = GameObject.Find("/Hand"); // This returns the GameObject named Hand,
// which is a child of Arm > Monster.
// Monster must not have a parent in the Hierarchy view.
hand = GameObject.Find("/Monster/Arm/Hand"); // This returns the GameObject named Hand,
// which is a child of Arm > Monster.
hand = GameObject.Find("Monster/Arm/Hand");
}
}
Example 3: how to reference this gameobject unity
// use the keyword "this"
this.SetActive(true);
// the line above will set the GameObject this script is attached to active