Search code examples
c#unity-game-engineboolean2dscene2d

After ending the scene need to start new scene


I'm doing an educational game for kids.. But I stopped at the end of the scene I could not make a code to start the new scene.. in the First Script when play game the scenes does not stop until the last scene.

YouWin.pictureInPlace++;

I searched a lot and did not find my question, so I consulted you. It was easier to do a button to go to the Next scene but I prefer to do it automaticly I think this task can be accomplished by the boolean but its need to reference game object.. and the script on 2 images. The First Script (Manager) has put on Four images At Canvas.. The second one (YouWin) I put on empty GameObject.. thanks for the help

The first script (Manger)

using UnityEngine;
using UnityEngine.EventSystems;

public class Manager : MonoBehaviour, IBeginDragHandler, IDragHandler, IEndDragHandler
{
    Vector2 pos1;
    public GameObject pos2;
    private bool canMove;
    public GameObject winner;
    void Start()
    {
        pos1 = transform.position;
        canMove = true;
    }

    public void OnBeginDrag(PointerEventData eventData)
    {
        Debug.Log(eventData);
    }

    public void OnDrag(PointerEventData eventData)
    {
        if (canMove)
            transform.position = Input.mousePosition;       
    }

    public void OnEndDrag(PointerEventData eventData)
    {
        float distance = Vector3.Distance(transform.position, pos2.transform.position);
        if (distance < 50)
        {
            transform.position = pos2.transform.position;
            transform.localScale = pos2.transform.localScale;
            canMove = false;
            winner.GetComponent<YouWin>().pictureInPlace++;   
        }
        else
        {
            transform.position = pos1;
        }
    }  
}

The second script (YouWin)

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

public class YouWin : MonoBehaviour
{
public int NumberOfImages;
public int pictureInPlace;
public string sceneName;

    void Update()
    {
        if (pictureInPlace == NumberOfImages)
        {
            StartCoroutine(LoadScene());
            Debug.Log("You Win!");
        }
    }

    IEnumerator LoadScene()
    {
       yield return new WaitForSeconds(1.5f);
        SceneManager.LoadScene(sceneName);

    }
}

Solution

  • After all thanks for helping me but I was able to find the right solution just make in the first Script (Manager) :

    bool static Done;
    void Start()
        {
    bool Done = false;
        }
     public void OnEndDrag(PointerEventData eventData)
        {
            float distance = Vector3.Distance(transform.position, pos2.transform.position);
            if (distance < 50)
            {
                transform.position = pos2.transform.position;
                transform.localScale = pos2.transform.localScale;
                canMove = false;
                bool Done = True; 
            }
    }
    

    and just call it from the second Script (YouWin)

    public string sceneName;
    void Update()
    {
    if(Youwin.Done)
    {
     StartCoroutine(LoadScene());
    }
    }
    
    IEnumerator LoadScene()
        {
    
            yield return new WaitForSeconds(0.5f);
            SceneManager.LoadScene(sceneName);
    
        }
    

    This IS The right solution ;