見出し画像

Unity シーン遷移スクリプトを30秒で実装!

こんにちは、やまりょうです!

Unityでシーンを移動させたい時、下記のコードを丸コピしてください!

必ず実装できます!


1.ボタンを押したらシーン移動

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;

public class LoadScene : MonoBehaviour
{
   // Start is called before the first frame update
   void Start()
   {
       
   }

   // Update is called once per frame
   void Update()
   {
       
   }

   public void OnLoad() //ボタンを押したら
   { 
       SceneManager.LoadScene("SampleScene"); //SampleSceneに移動
   }
}


2.コライダーの中にPlayerが侵入したらシーン移動

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;

public class LoadScene : MonoBehaviour
{
   // Start is called before the first frame update
   void Start()
   {
       
   }

   // Update is called once per frame
   void Update()
   {
       
   }

  public void OnTriggerEnter(Collider other)
   {
       if(other.gameObject.name == "Player")
       {
           SceneManager.LoadScene("SampleScene"); //"シーンの名前"
       }
   }
}


3.ボタンを押して5秒後にシーンを移動する

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;

public class LoadScene : MonoBehaviour
{
   // Start is called before the first frame update
   void Start()
   {
       
   }

   // Update is called once per frame
   void Update()
   {
       
   }

  public void OnLoad() //ボタンを押したら
   { 
       Invoke("Load",5.0f); //5秒後に
   }

   void Load()
   {
       SceneManager.LoadScene("SampleScene"); //SampleSceneに移動
   }
}

以上です!!

丸コピありです!

Unity Life楽しんでください!

この記事が気に入ったらサポートをしてみませんか?