MissingComponentException, but only after I play,MissingComponentException, but only after playing software.
Unity, whenever I hit play, decides that it need to reset all of the Components I have set in my GetComponent() functions. Before hitting play, all my settings are fine, all set. When I hit it, I have to reset them, or else get that error by pausing, setting them, and unpausing. When I stop hitting play, they go back to what they were before I started. I'll give my code, but I don't think that's it. Thanks in advance for your help.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
[RequireComponent(typeof(CharacterController))]
public class EpicScript : MonoBehaviour
{
// Public means you can edit it in the editor, you don't have to come back here.
public float speed = 15f;
public float lookSpeed = 2.0f;
public float lookXLimit = 60.0f;
public Light placeableObject;
public Transform Player;
public bool LightCollected = false;
public Light light;
public Interaction focus;
public Canvas Info;
public float lightLimit;
public float Lifelight = 10f;
public float untilTransition = 100f;
public float Transition = 0f;
//Things that can't be public
private float gravity = 20.0f;
CharacterController characterController;
public Camera camera;
Vector3 moveDirection = Vector3.zero;
Vector2 rotation = Vector2.zero;
[HideInInspector]
public bool canMove = true;
void Start()
{
// Basic Stuff
Cursor.lockState = CursorLockMode.Locked;
characterController = GetComponent<CharacterController>();
camera = GetComponent<Camera>();
rotation.y = transform.eulerAngles.y;
placeableObject = GetComponent<Light>();
Player = GetComponent<Transform>();
light = GetComponent<Light>();
light = placeableObject;
Info.enabled = false;
}
void Update()
{
Debug.Log(Time.time);
// Moving
if (characterController.isGrounded)
{
Vector3 forward = transform.TransformDirection(Vector3.forward);
Vector3 right = transform.TransformDirection(Vector3.right);
float curSpeedX = canMove ? speed * Input.GetAxis("Vertical") : 0;
float curSpeedY = canMove ? speed * Input.GetAxis("Horizontal") : 0;
moveDirection = (forward * curSpeedX) + (right * curSpeedY);
}
moveDirection.y -= gravity * Time.deltaTime;
characterController.Move(moveDirection * Time.deltaTime);
if (Input.GetKeyDown(KeyCode.E))
{
Ray ray = camera.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if(Physics.Raycast(ray, out hit, 100))
{
Interaction interactable = hit.collider.GetComponent<Interaction>();
if (interactable != null)
{
SetFocus(interactable);
LightCollected = true;
Info.enabled = true;
}
}
}
if (Input.GetKeyDown(KeyCode.Q) && LightCollected == true)
{
Info.enabled = false;
Destroy(Instantiate(placeableObject.gameObject, transform.position, transform.rotation), Lifelight);
}
}
void SetFocus(Interaction newFocus)
{
focus = newFocus;
}
void OnTriggerEnter()
{
SceneManager.LoadScene("The Outside World");
}
}
,Unity, everytime that I go to run my software that I have made, resets all of the components that I have set in the editor. I don't know why, but everytime I press the Play Button, it resets only certain settings, like what Camera I'm using for a Raycast, or what object I'm placing down using Instantiate(). I'll press play, and all of the things that I had set will reset, and when I stop, all of the settings will go back to what they were. I'll give my code, but I don't think it's that. I just don't know what to do anymore. Thanks in advance!
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
[RequireComponent(typeof(CharacterController))]
public class EpicScript : MonoBehaviour
{
// Public means you can edit it in the editor, you don't have to come back here.
public float speed = 15f;
public float lookSpeed = 2.0f;
public float lookXLimit = 60.0f;
public Light placeableObject;
public Transform Player;
public bool LightCollected = false;
public Light light;
public Interaction focus;
public Canvas Info;
public float lightLimit;
public float Lifelight = 10f;
public float untilTransition = 100f;
public float Transition = 0f;
//Things that can't be public
private float gravity = 20.0f;
CharacterController characterController;
public Camera camera;
Vector3 moveDirection = Vector3.zero;
Vector2 rotation = Vector2.zero;
[HideInInspector]
public bool canMove = true;
void Start()
{
// Basic Stuff
Cursor.lockState = CursorLockMode.Locked;
characterController = GetComponent<CharacterController>();
camera = GetComponent<Camera>();
rotation.y = transform.eulerAngles.y;
placeableObject = GetComponent<Light>();
Player = GetComponent<Transform>();
light = GetComponent<Light>();
light = placeableObject;
Info.enabled = false;
}
void Update()
{
Debug.Log(Time.time);
// Moving
if (characterController.isGrounded)
{
Vector3 forward = transform.TransformDirection(Vector3.forward);
Vector3 right = transform.TransformDirection(Vector3.right);
float curSpeedX = canMove ? speed * Input.GetAxis("Vertical") : 0;
float curSpeedY = canMove ? speed * Input.GetAxis("Horizontal") : 0;
moveDirection = (forward * curSpeedX) + (right * curSpeedY);
}
moveDirection.y -= gravity * Time.deltaTime;
characterController.Move(moveDirection * Time.deltaTime);
if (Input.GetKeyDown(KeyCode.E))
{
Ray ray = camera.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if(Physics.Raycast(ray, out hit, 100))
{
Interaction interactable = hit.collider.GetComponent<Interaction>();
if (interactable != null)
{
SetFocus(interactable);
LightCollected = true;
Info.enabled = true;
}
}
}
if (Input.GetKeyDown(KeyCode.Q) && LightCollected == true)
{
Info.enabled = false;
Destroy(Instantiate(placeableObject.gameObject, transform.position, transform.rotation), Lifelight);
}
}
void SetFocus(Interaction newFocus)
{
focus = newFocus;
}
void OnTriggerEnter()
{
SceneManager.LoadScene("The Outside World");
}
}
Your answer
Follow this Question
Related Questions
Time.deltaTime creating different speeds 1 Answer
Unexpected symbol error ? 1 Answer
Stupid question but I have to figure out 0 Answers
Parsing error 0 Answers