Static variable

유니티는 다른 씬으로 넘어가면 기본적으로 모든 오브젝트를 파괴한다. 그래서 다른 씬으로 넘어가면 이 전에 사용했던 변수나 함수를 사용하지 못한다.

using System.Collections;
using System.Collections.Generic;
using System.Threading;
using UnityEngine;
 
public class Dog : MonoBehaviour
{
    public string nickName;
    public float weight;
    // 전역변수 지정
    public static int count = 0;
    // 외부 오브젝트에서도 접근이 가능함. Dog.count
 
    private void Awake()
    {
        count += 1;
    }
    public void Bark()
    {
        Awake();
        Debug.Log("count : " + count + " " + nickName + "Bark!");
    }
 
    private void Start()
    {
        Bark();
    }
 
    public static void ShowDog()    // 이 함수도 역시 외부에서 접근이 가능함. Dog.ShowDog()
    {
        Debug.Log("Dog");
    }
}

https://8bitscoding.github.io/unity/09/

모든씬에서 사용할수 있는 전역변수 만들기