- Home /
Camera & Parent getting destroyed on Play
I implemented a player preview in my inventory system, and for some reason, the camera that sends the player view to the raw image in the inventory canvas gets deleted on play/runtime. Everybody I've asked says that it must be a delete function, but I don't have one in the player preview script. Only destroy function I have in the whole entire game is in the inventory script, where it destroys an item that I pickup, and the character whenever he dies.
Here's the display & inventory script:
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI;
public class Display : MonoBehaviour { //Constants private const int LAYER_DEFAULT = 0; private const int LAYER_DISPLAY = 9; private const float TURN = 360;
//Methods
private Transform target;
[SerializeField] private Transform displayAnchor;
[SerializeField] private Camera camera;
[SerializeField] private RectTransform display;
[SerializeField] private Canvas canvas;
[SerializeField] private LayerMask layer;
private RaycastHit hit;
private Transform pivot;
private Vector2 textureDimensions;
[SerializeField] private float rotationSensitivity = 15;
[SerializeField] private float minRotationX = -TURN;
[SerializeField] private float maxRotationX = TURN;
[SerializeField] private float minRotationY = -30;
[SerializeField] private float maxRotationY = 30;
private float rotationX;
private float rotationY;
private Quaternion originalRotation;
public bool IsDisplaying
{
get { return canvas.enabled; }
set { canvas.enabled = value; }
}
public Transform marker; //Debug
private bool isRotating;
private bool mouseHovering;
//Accessors
public Transform Target{
set{
if (target != null)
ChangeLayers(target, LAYER_DEFAULT);
target = value;
bool isNull = target == null;
pivot.gameObject.SetActive(!isNull);
displayAnchor.SetParent(target);
displayAnchor.localPosition = Vector3.zero;
if(!isNull)
{
ChangeLayers(target, LAYER_DISPLAY);
}
}
}
public bool IsRotating
{
set {isRotating = value;}
}
public bool MouseHovering
{
set { mouseHovering = value; }
}
private void Start ()
{
pivot = displayAnchor.GetChild(0);
textureDimensions = new Vector2(display.GetComponent<RawImage>().texture.width,
display.GetComponent<RawImage>().texture.height);
originalRotation = pivot.localRotation;
Target = GameObject.Find("Character").transform;
}
private void Update ()
{
if (Input.GetKeyDown(KeyCode.I))
IsDisplaying = !IsDisplaying;
if(IsDisplaying){
if (mouseHovering)
{
Vector3 scaledSizeDelta = display.sizeDelta * canvas.scaleFactor;
Vector3 bottomLeftCorner = new Vector3(display.position.x - (scaledSizeDelta.x / 2),
display.position.y - (scaledSizeDelta.y / 2));
hit = CastRay(camera, bottomLeftCorner, scaledSizeDelta, textureDimensions, layer);
}
if (isRotating)
RotatePivot(pivot);
}
}
private RaycastHit CastRay(Camera camera, Vector3 bottomLeftCorner, Vector3 sizeDelta, Vector2 textureDimensions, LayerMask layer)
{
Vector3 relativeMousePosition = Input.mousePosition - bottomLeftCorner;
relativeMousePosition.x = (relativeMousePosition.x / sizeDelta.x) * textureDimensions.x;
relativeMousePosition.y = (relativeMousePosition.y / sizeDelta.y) * textureDimensions.y;
RaycastHit hit;
//Casts the ray
Ray ray = camera.ScreenPointToRay(relativeMousePosition);
if (Physics.Raycast(ray, out hit, Mathf.Infinity, layer)){
Debug.DrawLine(camera.transform.position, hit.point, Color.blue);
marker.position = hit.point;
}
return hit;
}
public void OnClick()
{
if (!isRotating && hit.collider != null)
print(hit.collider.name);
}
private void RotatePivot(Transform pivot)
{
rotationX += Input.GetAxis("Mouse X") * rotationSensitivity;
rotationX += Input.GetAxis("Mouse Y") * rotationSensitivity;
rotationX = ClampAngle(rotationX, minRotationX, maxRotationX);
rotationX = ClampAngle(rotationY, minRotationY, maxRotationY);
Quaternion xQuaternion = Quaternion.AngleAxis(rotationX, Vector2.up);
Quaternion yQuaternion = Quaternion.AngleAxis(rotationY, Vector2.left);
pivot.localRotation = originalRotation * xQuaternion * yQuaternion;
}
//Static Mehods
public static float ClampAngle(float angle, float min, float max)
{
if (angle > TURN)
angle -= TURN;
if (angle < -TURN)
angle += TURN;
return Mathf.Clamp(angle, min, max);
}
public static void ChangeLayers(Transform target, int layer)
{
target.gameObject.layer = layer;
foreach (Transform child in target)
{
ChangeLayers(child, layer);
}
}
}
And inventory:
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class Inventory : MonoBehaviour { public GameObject inventory; public GameObject slotHolder; public GameObject itemManager; private bool inventoryEnabled;
private int slots;
private Transform[] slot;
private GameObject itemPickedUp;
private bool itemAdded;
private GameObject pickItem;
public Camera camera;
public float Range = 4f;
public void Start()
{
//slots being detected
slots = slotHolder.transform.childCount;
slot = new Transform[slots];
DetectInventorySlots ();
pickItem = GameObject.FindWithTag("Item");
}
public void Update()
{
if (Input.GetKeyDown (KeyCode.I))
{
inventoryEnabled = !inventoryEnabled;
}
if (inventoryEnabled)
inventory.GetComponent<Canvas> ().enabled = true;
else
inventory.GetComponent<Canvas> ().enabled = false;
if(inventoryEnabled && Input.GetKeyDown (KeyCode.Escape))
{
inventoryEnabled = false;
}
/*
if (Input.GetKeyDown (KeyCode.E))
{
itemPickedUp = pickItem;
AddItem(itemPickedUp);
}
*/ }
public void OnTriggerEnter(Collider other)
{
if (other.tag == "Item"){
itemPickedUp = other.gameObject;
AddItem (itemPickedUp);
}
}
public void OnTriggerExit(Collider other)
{
if (other.tag == "Item") {
itemAdded = false;
}
}
public void AddItem(GameObject item)
{
for (int i = 0; i < slots; i++)
{
if (slot [i].GetComponent<Slot> ().empty && itemAdded == false)
{
slot [i].GetComponent<Slot> ().item = itemPickedUp;
slot [i].GetComponent<Slot> ().itemIcon = itemPickedUp.GetComponent<Item> ().icon;
item.transform.parent = itemManager.transform;
item.transform.position = itemManager.transform.position;
item.transform.localPosition = item.GetComponent<Item> ().position;
item.transform.localEulerAngles = item.GetComponent<Item> ().rotation;
item.transform.localScale = item.GetComponent<Item> ().scale;
Destroy(item.GetComponent<Rigidbody>());
itemAdded = true;
item.SetActive (false);
}
}
}
public void DetectInventorySlots()
{
for (int i = 0; i < slots; i++)
{
slot [i] = slotHolder.transform.GetChild (i);
}
inventory.GetComponent<Canvas> ().enabled = true;
}
}
Your answer
Follow this Question
Related Questions
How to access camera preview frames with MediaCapture Class in Unity? 0 Answers
Why are my player controls wierd. 1 Answer
I need help to make the player go towards where the camera is facing. 1 Answer
Multiple Cars not working 1 Answer
How can i make my player moving towards the direction where my main camera is facing? 2 Answers