Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by ealva479 · Apr 27, 2018 at 10:08 PM · c#cameraplayerinventory systempreview

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;
 }

}

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

226 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges