Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Object Becoming Null after Click

Discussion in 'Scripting' started by rooflesnack, May 16, 2020.

  1. rooflesnack

    rooflesnack

    Joined:
    Jan 26, 2016
    Posts:
    3
    Here is the related code:

    Code (CSharp):
    1. Debug.Log(InventoryManager.Instance.From + "This is my from right now");
    2.         if (InventoryManager.Instance.HoverObject != null)
    3.         {
    4.             Vector2 position;
    5. RectTransformUtility.ScreenPointToLocalPointInRectangle(InventoryManager.Instance.Canvas.transform as RectTransform, Input.mousePosition, null, out position);
    6.             position.Set(position.x, position.y - hoverYOffset);
    7.             InventoryManager.Instance.HoverObject.transform.position = InventoryManager.Instance.Canvas.transform.TransformPoint(position);
    8.         }
    9.  
    10.         if (Input.GetMouseButtonUp (0))
    11.         {
    12.  
    13.             Debug.Log(InventoryManager.Instance.From + "This is my from after clicking");
    14.             if (!isMouseOver && InventoryManager.Instance.From != null)...
    Somehow that first debug line comes back as SlotType, which is expected behavior after I select an item from the inventory window. Without doing anything else, I click away from the inventory window and suddenly InventoryManager.Instance.From is null!

    Worse, this problem sometimes resolves itself after I have been playing for a little while, but not on every run of the game. I am so confused right now what could possibly be happening.

    All of this code is inside of my Update function.

    Apologies for my code formatting, I'm not sure why that line won't indent for me.
     
  2. rooflesnack

    rooflesnack

    Joined:
    Jan 26, 2016
    Posts:
    3
    Scratch that, there was another onclick event in my equipment window that I wasn't accounting for that was setting my From slot to null.
     
  3. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,890
    What type of object is "From"?

    Basically one of two things is happening:
    • You are setting InventoryManager.Instance.From = null somewhere else in your code
    • You are Destroy()-ing either the From object or the GameObject it is attached to (or one of its parents).
    Are you destroying anything in your code?

    Edit: yep
     
  4. rooflesnack

    rooflesnack

    Joined:
    Jan 26, 2016
    Posts:
    3
    Haha but thank you anyway :)