Loading...
Loading...
Unity 6 core concepts and architecture guide. Use when working with GameObjects, Components, Transforms, Scenes, Prefabs, ScriptableObjects, or Unity project structure. Covers the entity-component architecture, object hierarchy, tags, layers, and project conventions. Based on Unity 6.3 LTS documentation.
npx skill4agent add nice-wolf-studio/unity-claude-skills unity-foundations(0,0,0)UnityEngine.Object.assetEditorUtility.SetDirty()UntaggedRespawnFinishEditorOnlyMainCameraPlayerGameControllerMainCameraCamera.mainEditorOnlyusing UnityEngine;
public class ComponentAccess : MonoBehaviour
{
void Start()
{
// Get a component on this GameObject
Rigidbody rb = GetComponent<Rigidbody>();
// Get a component on a child GameObject
Collider childCollider = GetComponentInChildren<Collider>();
// Get all components of a type on this and children
MeshRenderer[] renderers = GetComponentsInChildren<MeshRenderer>();
// Add a component at runtime
BoxCollider box = gameObject.AddComponent<BoxCollider>();
// Remove a component (destroys it)
Destroy(box);
}
}using UnityEngine;
public class FindingObjects : MonoBehaviour
{
void Start()
{
// Find by tag (returns first match)
GameObject player = GameObject.FindWithTag("Player");
// Find all with tag
GameObject[] enemies = GameObject.FindGameObjectsWithTag("Enemy");
// Find by name (slow -- avoid in Update)
GameObject manager = GameObject.Find("GameManager");
// Compare tags efficiently (no GC allocation)
if (gameObject.CompareTag("Player"))
{
Debug.Log("This is the player");
}
}
}using UnityEngine;
public class InstantiationExample : MonoBehaviour
{
// Reference to the prefab. Drag a prefab into this field in the Inspector.
public GameObject myPrefab;
void Start()
{
// Instantiate at position (0, 0, 0) and zero rotation.
Instantiate(myPrefab, new Vector3(0, 0, 0), Quaternion.identity);
}
}using UnityEngine;
public class SpawnWithParent : MonoBehaviour
{
public GameObject prefab;
public Transform parentTransform;
void SpawnChild()
{
// Instantiate as child of a parent transform
GameObject instance = Instantiate(prefab, parentTransform);
// Instantiate at specific world position under a parent
GameObject positioned = Instantiate(
prefab,
new Vector3(5, 0, 0),
Quaternion.identity,
parentTransform
);
}
}using UnityEngine;
[CreateAssetMenu(fileName = "Data", menuName = "ScriptableObjects/SpawnData", order = 1)]
public class SpawnDataScriptableObject : ScriptableObject
{
public GameObject prefab;
public int count;
public Vector3[] positions;
}using UnityEngine;
public class SpawnManager : MonoBehaviour
{
public SpawnDataScriptableObject spawnData;
void Start()
{
for (int i = 0; i < spawnData.count; i++)
{
Instantiate(spawnData.prefab, spawnData.positions[i], Quaternion.identity);
}
}
}using UnityEngine;
using UnityEngine.SceneManagement;
public class SceneLoader : MonoBehaviour
{
void Start()
{
// Subscribe to scene loaded event
SceneManager.sceneLoaded += OnSceneLoaded;
}
void OnSceneLoaded(Scene scene, LoadSceneMode mode)
{
Debug.Log("Loaded: " + scene.name);
}
public void LoadLevel(string sceneName)
{
// Load scene by name (replaces current)
SceneManager.LoadScene(sceneName);
}
public void LoadAdditive(string sceneName)
{
// Load scene additively (keeps current scenes)
SceneManager.LoadSceneAsync(sceneName, LoadSceneMode.Additive);
}
public void UnloadLevel(string sceneName)
{
SceneManager.UnloadSceneAsync(sceneName);
}
public void GetSceneInfo()
{
Scene active = SceneManager.GetActiveScene();
Debug.Log("Active scene: " + active.name);
Debug.Log("Loaded scene count: " + SceneManager.loadedSceneCount);
}
}using UnityEngine;
public class RespawnSystem : MonoBehaviour
{
public GameObject respawnPrefab;
private GameObject respawn;
void Update()
{
if (respawn == null)
respawn = GameObject.FindWithTag("Respawn");
if (respawn != null)
{
Instantiate(respawnPrefab, respawn.transform.position,
respawn.transform.rotation);
}
}
}using UnityEngine;
public class ToggleVisibility : MonoBehaviour
{
public GameObject target;
public void Toggle()
{
// SetActive controls whether the GameObject is active
target.SetActive(!target.activeSelf);
// activeSelf: this object's own active state
// activeInHierarchy: effective state (considers parent chain)
Debug.Log("Self: " + target.activeSelf);
Debug.Log("InHierarchy: " + target.activeInHierarchy);
}
}using UnityEngine;
public class LayerRaycast : MonoBehaviour
{
void Update()
{
// Create a layer mask for layer named "Ground"
int groundLayer = LayerMask.NameToLayer("Ground");
int layerMask = 1 << groundLayer;
// Raycast only against the Ground layer
if (Physics.Raycast(transform.position, Vector3.down, out RaycastHit hit, 100f, layerMask))
{
Debug.Log("Hit ground at: " + hit.point);
}
// Use GetMask for multiple layers
int combinedMask = LayerMask.GetMask("Ground", "Water");
Physics.Raycast(transform.position, Vector3.forward, 50f, combinedMask);
}
}// BAD: Find is expensive -- runs every frame with string lookup
void Update()
{
GameObject player = GameObject.Find("Player"); // Avoid!
}
// GOOD: Cache the reference
private GameObject player;
void Start()
{
player = GameObject.FindWithTag("Player");
}// BAD: Every prefab instance duplicates this data
public class EnemyStats : MonoBehaviour
{
public int health = 100;
public float speed = 5f;
public string enemyName = "Goblin";
}
// GOOD: Use a ScriptableObject -- one asset, many references
[CreateAssetMenu(menuName = "ScriptableObjects/EnemyConfig")]
public class EnemyConfig : ScriptableObject
{
public int health = 100;
public float speed = 5f;
public string enemyName = "Goblin";
}
public class Enemy : MonoBehaviour
{
public EnemyConfig config; // All instances share the same asset
}// BAD: Changes in Edit mode won't persist
settings.value += 10;
// GOOD: Mark asset dirty so Unity saves it
settings.value += 10;
EditorUtility.SetDirty(settings);// BAD: Allocates a new string for comparison (GC pressure)
if (gameObject.tag == "Player") { }
// GOOD: CompareTag avoids allocation
if (gameObject.CompareTag("Player")) { }(0,0,0)| API | Description | Notes |
|---|---|---|
| Get component on same GameObject | Returns null if not found |
| Get component on self or children | Searches depth-first |
| Get all matching components in hierarchy | Returns array |
| Attach new component at runtime | Returns the new component |
| Destroy a GameObject or Component | Deferred to end of frame |
| Clone a prefab at position/rotation | Returns the clone |
| Find first active GO with tag | Returns null if none |
| Find all active GOs with tag | Returns array |
| Find by name (expensive) | Avoid in Update loops |
| Activate/deactivate | Disables all components |
| Tag comparison without GC alloc | Preferred over |
| Load scene (replaces current) | Must be in Build Settings |
| Async scene loading | Additive or Single mode |
| Unload a loaded scene | Returns AsyncOperation |
| Get current active scene | Returns Scene struct |
| Get layer index from name | Returns int |
| Get combined mask from layer names | Params string array |
| Get MainCamera-tagged camera | Cached by Unity |