Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Picking Up Items Crashes My Game!?

Discussion in 'Scripting' started by Treasureman, Apr 1, 2016.

  1. Treasureman

    Treasureman

    Joined:
    Jul 5, 2014
    Posts:
    563
    I have a game where picking u certain items is essential. But, for some reason, when I try to pick up an item in the Unity Engine, It always crashes, and when I play the game in a full build, I can pick up the items half the time, but the other half of the time, my game just crashes. My equipable objects have 2 scripts on them. One that makes the item on the ground disappear...
    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class ItemDisappear : MonoBehaviour {
    4.  
    5.     public GameObject Item;
    6.  
    7.     void OnTriggerStay(Collider other)
    8.     {
    9.         if (Input.GetKeyDown("f") && other.gameObject.name == "First Person Controller")
    10.         {
    11.             Debug.Log(other.gameObject.name + " has triggered " + gameObject.name + " to be set inactive.");
    12.             Item.SetActive(false);
    13.         }
    14.     }
    15. }
    16.  
    And another code that makes the object in your hand appear (it's the same thing, but says true instead of false)...
    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class ItemDisappear : MonoBehaviour {
    4.  
    5.     public GameObject Item;
    6.  
    7.     void OnTriggerStay(Collider other)
    8.     {
    9.         if (Input.GetKeyDown("f") && other.gameObject.name == "First Person Controller")
    10.         {
    11.             Debug.Log(other.gameObject.name + " has triggered " + gameObject.name + " to be set inactive.");
    12.             Item.SetActive(true);
    13.         }
    14.     }
    15. }
    16.  
    Why is this causing my game to crash? The combined codes can't be that data consuming, can they?
     
  2. caseyboundless

    caseyboundless

    Joined:
    Oct 17, 2011
    Posts:
    590
    Code (CSharp):
    1. void OnTriggerEnter(Collider other)
    2.     {
    3.         if (Input.GetKeyDown("f") && other.gameObject.name == "First Person Controller")
    4.         {
    5.             Debug.Log(other.gameObject.name + " has triggered " + gameObject.name + " to be set inactive.");
    6.             Item.SetActive(true);
    7.         }
    8.     }
     
  3. Treasureman

    Treasureman

    Joined:
    Jul 5, 2014
    Posts:
    563
    What's the difference?
     
  4. caseyboundless

    caseyboundless

    Joined:
    Oct 17, 2011
    Posts:
    590
    OnTriggerStay is more expensive on processing than OnTriggerEnter. Are you just wanting your character to walk on an obj and make it disappear?
     
  5. Treasureman

    Treasureman

    Joined:
    Jul 5, 2014
    Posts:
    563
    No, it works by having the player entering the collider and then pressing F.
     
  6. Zaladur

    Zaladur

    Joined:
    Oct 20, 2012
    Posts:
    392
    That is going to be hard to achieve - OnTriggerEnter combined with a GetKeyDown means you need the press the key at the exact frame that the item gets in range.

    OP, I assume that the secondScript is called ItemAppear, or something similar? Both classes are named the same in your example.

    Does the Item gameObject have any scripts on it? If so, I would remove them and use a primitive in their place to make sure that is not the issue.

    Also, you could use OnTriggerEnter and OnTriggerExit to just change the State of the equipment. Flip a bool flag when you enter or exit range, and run your Input checks in Update only when the flag is true. This can be a better alternative than checking inputs in OnTriggerStay.
     
  7. caseyboundless

    caseyboundless

    Joined:
    Oct 17, 2011
    Posts:
    590
    Code (CSharp):
    1. public bool isTouching;
    2. void Update()
    3. {
    4. MyInput();
    5. }
    6.  
    7. void MyInput()
    8. {
    9.   If(Input.GetKeyDown(KeyCode.F) && isTouching == true)
    10. {
    11.   item.SetActive(true);
    12. }
    13. }
    14.  
    15.     void OnTriggerEnter(Collider other)
    16.         {
    17.             if (other.gameObject.name == "First Person Controller")
    18.             {
    19.                 isTouching = true;
    20.             }
    21.         }
     
  8. Treasureman

    Treasureman

    Joined:
    Jul 5, 2014
    Posts:
    563
    What do you mean a primitive? The Item is the only thing with the scripts on it. The one in your hands doesn't have any related scripts.
     
  9. Treasureman

    Treasureman

    Joined:
    Jul 5, 2014
    Posts:
    563
    I got these errors...

    Assets/ItemDisappear.cs(15,70): error CS1525: Unexpected symbol `{'
    Assets/ItemDisappear.cs(20,14): error CS0116: A namespace can only contain types and namespace declarations
    Assets/ItemDisappear.cs(27,1): error CS8025: Parsing error

    Here's the rewritten script...
    Code (CSharp):
    1. sing UnityEngine;
    2.  
    3. public class ItemDisappear : MonoBehaviour {
    4.  
    5.     public GameObject item;
    6.  
    7.     public bool isTouching;
    8.     void Update()
    9.     {
    10.         MyInput();
    11.     }
    12.  
    13.     void MyInput()
    14.     {
    15.         If(Input.GetKeyDown(KeyCode.F) && isTouching == true){
    16.             item.SetActive(false);
    17.         }
    18.     }
    19.  
    20.     void OnTriggerEnter(Collider other)
    21.     {
    22.         if (other.gameObject.name == "First Person Controller")
    23.         {
    24.             isTouching = true;
    25.         }
    26.     }
    27. }
     
  10. caseyboundless

    caseyboundless

    Joined:
    Oct 17, 2011
    Posts:
    590
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class ItemDisapear : MonoBehaviour {
    5.  
    6.     public GameObject item;
    7.  
    8.     public bool isTouching;
    9.  
    10.     void Update()
    11.     {
    12.         MyInput();
    13.     }
    14.  
    15.     void MyInput()
    16.     {
    17.         if(Input.GetKeyDown(KeyCode.F) && isTouching == true)
    18.         {
    19.             item.SetActive(false);
    20.         }
    21.     }
    22.  
    23.     void OnTriggerEnter(Collider other)
    24.     {
    25.         if (other.gameObject.name == "First Person Controller")
    26.         {
    27.             isTouching = true;
    28.         }
    29.     }
    30. }