Last active
December 28, 2023 03:36
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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