Quantcast
Channel: Questions in topic: "exception"
Viewing all articles
Browse latest Browse all 470

FindWithTag returns NullReferenceException

$
0
0
Hello, I have readed a lot of forums about this, and lots of answers, but I can not still solve this problem. When I have 1 door in my scene, everything is OK, but when I add another one, I am getting NullReferenceException: My code: using UnityEngine; using System.Collections; public class Door : MonoBehaviour { public GameObject doorBody; GameObject useText; Transform player; private AudioSource doorSound; public float doorOpenAngle = 90.0f; public float doorCloseAngle = 0.0f; public float doorAnimSpeed = 2.0f; private Quaternion doorOpen = Quaternion.identity; private Quaternion doorClose = Quaternion.identity; private Transform playerTrans = null; public bool doorStatus = false; //false = close, true = open private bool doorGo = false; void Start() { doorStatus = false; //door is open doorSound = GetComponent(); doorOpen = Quaternion.Euler(0, doorOpenAngle, 0); doorClose = Quaternion.Euler(0, doorCloseAngle, 0); playerTrans = GameObject.FindWithTag("Player").transform; Debug.Log(playerTrans.position); useText = GameObject.FindWithTag("Use"); useText.SetActive(false); } void Update() { if (Vector3.Distance(playerTrans.position, this.transform.position) < 2f) { useText.SetActive(true); if (Input.GetKeyDown(KeyCode.E) && !doorGo) { doorBody.GetComponent().enabled = false; doorSound.mute = false; if (doorStatus) { //close door StartCoroutine(this.moveDoor(doorClose)); doorSound.Play(); } else { //open door StartCoroutine(this.moveDoor(doorOpen)); doorSound.Play(); } } } else { useText.SetActive(false); } } public IEnumerator moveDoor(Quaternion dest) { doorGo = true; while (Quaternion.Angle(transform.localRotation, dest) > 4.0f) { transform.localRotation = Quaternion.Slerp(transform.localRotation, dest, Time.deltaTime * doorAnimSpeed); yield return null; } //Change door status doorBody.GetComponent().enabled = true; doorStatus = !doorStatus; doorGo = false; yield return null; } } Exception: > Severity Code Description Project File Line Suppression> State Error NullReferenceException:> Object reference not set to an> instance of an object Solution 'Shadow> of Fear' ‎(3 > projects) Assets/Own/Scripts/Door.cs 49 [1]: /storage/temp/67689-error.jpg Thank you :)

Viewing all articles
Browse latest Browse all 470

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>