unity as 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 create gameobject

// Use 'new GameObject' to create a new GameObject in the current scene
SpawnedObject = new GameObject("Created GameObject");

Example 3: create gameobject unity

using UnityEngine;
public class InstantiationExample : MonoBehaviour 
{
    // Reference to the Prefab. Drag a Prefab into this field in the Inspector.
    public GameObject myPrefab;

    // This script will simply instantiate the Prefab when the game starts.
    void Start()
    {
        // Instantiate at position (0, 0, 0) and zero rotation.
        Instantiate(myPrefab, new Vector3(0, 0, 0), Quaternion.identity);
    }
}

Example 4: 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