Skip to content

Instantly share code, notes, and snippets.

@keithweaver
Last active December 28, 2023 03:36
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save keithweaver/c26425b11d5c2db74b2716fa3ffba979 to your computer and use it in GitHub Desktop.
Save keithweaver/c26425b11d5c2db74b2716fa3ffba979 to your computer and use it in GitHub Desktop.
Create a new game object/prefab in a scene using C# code for Unity.
using UnityEngine;
using System.Collections;
// I attached this script to my main camera
public class GenerateMap : MonoBehaviour {
void Start () {
int x = 0;
int y = 0;
// Adding a Prefab/GameObject to Scene using C#. Make sure you create a prefab with the file name
// "platform-lg" and put it in /assets/resources/
//GameObject cube = GameObject.CreatePrimitive(PrimitiveType.Cube); // Create basic cube instead of prefab
GameObject cube = (GameObject)Instantiate(Resources.Load("platform-lg"));
cube.AddComponent<Collider2D>();
// cube.AddComponent<Rigidbody>(); // add rigid body
cube.transform.position = new Vector3(x, y, 0);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment