Unity script ile script kapatma

Calimero

Hectopat
Katılım
19 Aralık 2020
Mesajlar
119
Daha fazla  
Cinsiyet
Erkek
360 sanal müze projemde kupa inceleme scripti yazıyorum fakat menu scriptim ile sorun çıkarıyor. Eğer bir kupayı incelerken menu açılırsa ve menu kapatılırsa kamera scripti inceleme modunda aktifleşiyor. Ifler ile düzeltmeyi denedim fakat olmadı, son çare buraya yazmak geldi aklıma. Yardım edenlere şimdiden teşekkür ederim.
Menü kodları:

C#:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class MenuController : MonoBehaviour.
{
 public static bool gameIsPaused = false;
 public GameObject mainCamera;
 public GameObject gamePanel;
 public GameObject pauseMenu;

 public TrophyInteract trophyInteract; // TrophyInteract scriptine referans.

 //toggle bar ve muzik tanımlandı.
 public Toggle musicToggle;
 public AudioSource musicAudioSource;
 private MSCameraController cameraManager;

 void Start()
 {
 cameraManager = mainCamera.GetComponent<MSCameraController>();
 // TrophyInteract scriptine referansı atama.
 trophyInteract = mainCamera.GetComponent<TrophyInteract>();
 // Toggle'ın değisim olayını dinle.
 musicToggle.onValueChanged.AddListener(delegate
 {
 OnMusicToggleValueChanged(musicToggle);
 });
 }

 void Update()
 {
 if (Input.GetKeyDown(KeyCode.Escape))
 {
 if (gameIsPaused == false)
 {
 Pause();
 }
 else.
 {
 Resume();
 }
 }
 }

 public void Resume()
 {
 Cursor.lockState = CursorLockMode.Locked;
 pauseMenu.SetActive(false);
 gamePanel.SetActive(true);
 gameIsPaused = false;

 if (!pauseMenu.activeSelf || !trophyInteract.enabled)
 {
 mainCamera.GetComponent<MSCameraController>().enabled = true;
 }
 }

 public void Pause()
 {
 Cursor.lockState = CursorLockMode.Confined;
 pauseMenu.SetActive(true);
 gamePanel.SetActive(false);
 gameIsPaused = true;

 mainCamera.GetComponent<MSCameraController>().enabled = false;
 }

 public void Shutdown()
 {
 Application.Quit();
 }

 private void OnMusicToggleValueChanged(Toggle change)
 {
 // Toggle'ın durumuna göre müziği aç veya kapat.
 if (change.isOn)
 {
 // Toggle açıksa, müziği normal seviyede çal.
 musicAudioSource.volume = 1f;
 musicAudioSource.UnPause();
 }
 else.
 {
 // Toggle kapalıysa, müziği durdur.
 musicAudioSource.volume = 0f;
 musicAudioSource.Pause();
 }
 }
}

Inceleme kodları:

C#:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class TrophyInteract : MonoBehaviour.
{
 [Header("Trophy")]
 public Transform TrophyInspectPosition;
 [SerializeField] public List<Trophy> Trophies;
 [Header("UI")]
 public GameObject PauseMenu;
 public GameObject TimerPanel;
 public Text TrophyNameText;
 private MSCameraController _CameraManager;
 private WorldManager _WorldManager;
 [Header("Camera")]
 public Camera MainCamera;
 public float RaycastLength;

 public Trophy CurrentTrophy = null;

 bool trophyInspected = false;

 void Start()
 {
 CurrentTrophy = null;
 _CameraManager = MainCamera.GetComponent<MSCameraController>();
 _WorldManager = MainCamera.GetComponent<WorldManager>();

 foreach (var Trophy in Trophies)
 {
 Trophy.TrophyStartPosition = Trophy.TrophyPrefab.transform.position;
 Trophy.TrophyStartRotation = Trophy.TrophyPrefab.transform.rotation;
 Trophy.TrophyManager = Trophy.TrophyPrefab.GetComponent<TrophyManager>();
 }
 }

 bool trophiesReleased = false;
 public bool IsTropyhNull(Trophy trophy)
 {
 if (trophy == null)
 {
 return true;
 }
 else if (trophy.TrophyManager == null || trophy.TrophyPrefab == null)
 {
 return true;
 }
 else.
 {
 return false;
 }
 }

 void Update()
 {
 bool TrophyHit = false;
 foreach (Trophy Trophy in Trophies)
 {
 TrophyHit = CheckCollision(Trophy);

 if (TrophyHit)
 {
 TrophyNameText.text = Trophy.TrophyName;
 Debug.Log("Hit!");
 if ((Input.GetMouseButtonDown(1) && PauseMenu.activeInHierarchy == false))
 {
 if (Trophy.IsSelected)
 {
 ReleaseTrophies();
 TimerPanel.SetActive(false);
 trophiesReleased = true;
 }
 }
 if ((Input.GetMouseButtonDown(0) && PauseMenu.activeInHierarchy == false))
 {
 TimerPanel.SetActive(true);
 InspectTrophy(Trophy);
 }
 }
 else if (!TrophyHit && Trophy.TrophyPrefab.activeInHierarchy)
 {
 Debug.Log("No!");
 }
 }

 if (!trophiesReleased && IsTropyhNull(CurrentTrophy))
 {
 Debug.Log("TrophyPrefab is not active. Releasing trophies.");
 ReleaseTrophies();
 }
 }

 public void InspectTrophy(Trophy trophy)
 {
 MainCamera.transform.rotation = Quaternion.Euler(Vector3.zero);
 _CameraManager.enabled = false;
 _WorldManager.enabled = false;

 CurrentTrophy = trophy;
 trophy.TrophyPrefab.transform.position = TrophyInspectPosition.position;
 trophy.TrophyManager.enabled = true;
 trophy.IsSelected = true;

 trophyInspected = true;
 }

 public void ReleaseTrophies()
 {
 if (trophyInspected)
 {
 _CameraManager.enabled = true;
 _WorldManager.enabled = true;

 foreach (var Trophy in Trophies)
 {
 Trophy.TrophyPrefab.transform.position = Trophy.TrophyStartPosition;
 Trophy.TrophyPrefab.transform.rotation = Trophy.TrophyStartRotation;
 Trophy.TrophyManager.enabled = false;
 Trophy.IsSelected = false;
 }
 CurrentTrophy = null;
 }

 trophyInspected = false;
 }

 bool CheckCollision(Trophy trophy)
 {
 if (trophy.TrophyPrefab == null)
 {
 Debug.LogError("TrophyPrefab is null in the Trophy object.");
 return false;
 }

 Ray ray = new Ray(transform.position, transform.forward);
 RaycastHit hit;

 if (Physics.Raycast(ray, out hit, RaycastLength) && hit.collider.CompareTag(trophy.TrophyPrefab.tag))
 {
 Debug.DrawLine(transform.position, hit.point, Color.red);
 return true;
 }

 return false;
 }
}

[System.Serializable]
public class Trophy.
{
 public string TrophyName;
 public string TrophyDescription;
 public bool IsSelected;
 public GameObject TrophyPrefab;
 public Vector3 TrophyStartPosition;
 public Quaternion TrophyStartRotation;
 public TrophyManager TrophyManager;
}
 
Son düzenleyen: Moderatör:

Geri
Yukarı