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

Help with my item pickup script.

Discussion in 'Scripting' started by Treasureman, Feb 10, 2016.

  1. Treasureman

    Treasureman

    Joined:
    Jul 5, 2014
    Posts:
    563
    I have a script that I want to make an object disappear when you go inside of its colldier and press F. Here's the script...
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class ItemDisappear : MonoBehaviour {
    5.  
    6. public GameObject item;
    7.  
    8.     void OnTriggerEnter(Collider item)
    9.         if(Input.GetKey("f")){
    10.  
    11.         item.gameObject.SetActive(false);
    12.         //or
    13.         item.GetComponent<Renderer>().enabled = false;
    14.     }
    15. }
    But I get a bunch of errors. Probably some missing brackets or something, but I'm having trouble seeing it...

    Assets/ItemDisappear.cs(9,32): error CS1519: Unexpected symbol `(' in class, struct, or interface member declaration
    Assets/ItemDisappear.cs(11,42): error CS1519: Unexpected symbol `(' in class, struct, or interface member declaration
    Assets/ItemDisappear.cs(13,44): error CS1519: Unexpected symbol `(' in class, struct, or interface member declaration
    Assets/ItemDisappear.cs(13,55): error CS1519: Unexpected symbol `=' in class, struct, or interface member declaration
    Assets/ItemDisappear.cs(15,1): error CS8025: Parsing error

    What do I do to fix it?
     
  2. GamesOnAcid

    GamesOnAcid

    Joined:
    Jan 11, 2016
    Posts:
    283
    Use brackets
    OnTriggerEnter(Collider item){

    }
     
    LeftyRighty likes this.
  3. Treasureman

    Treasureman

    Joined:
    Jul 5, 2014
    Posts:
    563
    Alright, I fixed it, but I doesn't do anything. What's wrong with it?
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class ItemDisappear : MonoBehaviour {
    5.  
    6. public GameObject item;
    7.  
    8.     void OnTriggerEnter(Collider item){
    9.         if (Input.GetKey ("f")) {
    10.  
    11.             item.gameObject.SetActive (false);
    12.             //or
    13.             item.GetComponent<Renderer> ().enabled = false;
    14.         }
    15.     }
    16. }
     
  4. GamesOnAcid

    GamesOnAcid

    Joined:
    Jan 11, 2016
    Posts:
    283
    I believe you problem lies in the OnTriggerEnter function. OnTriggerEnter is only called one frame (when you enter the trigger). Try using OnTriggerStay. If that doesn't work, I don't have full access to the project, but here are some recommendations:
    Make sure that the script is attached to the GameObject properly, and has colliders set as triggers.
    I would use Input.GetKeyDown, as it triggers on the frame it detects the key is down, but it might not change anything.
    also
    Code (CSharp):
    1. item.gameObject.SetActive (false);
    2.             //or
    3.             item.GetComponent<Renderer> ().enabled = false;
    Are these achieving the same effect? If so, you don't need to use both of them.
     
  5. Gilmar

    Gilmar

    Joined:
    Dec 17, 2013
    Posts:
    30
    try changing OnTriggerEnter to OnTriggerStay
     
  6. Treasureman

    Treasureman

    Joined:
    Jul 5, 2014
    Posts:
    563
    Thank you so much, really helped
     
    GamesOnAcid likes this.
  7. GamesOnAcid

    GamesOnAcid

    Joined:
    Jan 11, 2016
    Posts:
    283
    Not a problem
     
  8. Treasureman

    Treasureman

    Joined:
    Jul 5, 2014
    Posts:
    563
    Actually, I ran into another issue. I attached it to my player and dragged the item I want to disappear into the item variable. But now, It makes every single object with an IsTrigger Collider disappear when I go over to it and press F, including doors. Why?
    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class ItemDisappear : MonoBehaviour {
    5.  
    6. public GameObject item;
    7.  
    8.     void OnTriggerStay(Collider item){
    9.         if (Input.GetKeyDown ("f")) {
    10.  
    11.             item.gameObject.SetActive (false);
    12.         }
    13.     }
    14. }
     
  9. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    scope... the "item" in the function is referring to the parameter you have called "item", not the class variable "item".
     
  10. GamesOnAcid

    GamesOnAcid

    Joined:
    Jan 11, 2016
    Posts:
    283
    It could also just be that since it's on your player, it's triggering the script with all gameObjects. Try attaching the script to the object you want to disappear when you pick it up, that way it will only use it's own trigger, which will be destroyed when activated with 'f'
     
  11. Treasureman

    Treasureman

    Joined:
    Jul 5, 2014
    Posts:
    563
    It worked with not making the doors disappear, but instead of the item disappearing, the player does
     
  12. GamesOnAcid

    GamesOnAcid

    Joined:
    Jan 11, 2016
    Posts:
    283
    Did you put the script on the GameObject you want to disappear and not the player? if so, I'm not mainly a c# programmer, so it might just be something small that I can't see either. Try making the item variable a little less vague, just in case it's conflicting with something else. Call it more specifically.
     
  13. BluHornet

    BluHornet

    Joined:
    Feb 23, 2015
    Posts:
    40
    ok the problem here is this script is on the item you want to disappear. the "item" passed into the OnTriggerStay is the collider that entered the trigger (the player). so what is actually happening is the object to be picked up is there and the player walks in and hits the f key. the script says "hey you inside my trigger! Turn off!" that being the player then turns off. what you should do is turn off the item itself. try something like this.
    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class ItemDisappear : MonoBehaviour {
    4.  
    5.     void OnTriggerStay(Collider other)
    6.     {
    7.         if (Input.GetKeyDown("f") && other.gameObject.name == "Player")
    8.         {
    9.            gameObject.SetActive(false);
    10.         }
    11.     }
    12. }
    13.  
    all you have to do is either name the player's game object "Player" in the inspector or change "Player" to whatever you named your player. The difference here is that when the item to disappear has something in it's trigger it waits for the f key. when the f key is pressed it makes sure the player is inside it's trigger and it he/she is then it turns itself off.
     
  14. Treasureman

    Treasureman

    Joined:
    Jul 5, 2014
    Posts:
    563
    The script doesn't do anything. Am I supposed to add something?
     
  15. BluHornet

    BluHornet

    Joined:
    Feb 23, 2015
    Posts:
    40
    line 7 in the " " must be exactly the same as the player game object's name in the inspector.
    the script must be on the object you want to disappear.
    the object you want to disappear must have a collider that is set as a trigger.

    if all the above is true try replacing line 9 with
    Code (CSharp):
    1. Debug.Log(other.gameObject.name + " has triggered " + gameObject.name + " to be set inactive.");
    2. gameObject.SetActive(false);
    and see what you get in the console.
     
  16. Treasureman

    Treasureman

    Joined:
    Jul 5, 2014
    Posts:
    563
    It still doesn't do anything. I'm new to CSharp, and I'm not sure if I have to write anything in to fit my script.
     
  17. BluHornet

    BluHornet

    Joined:
    Feb 23, 2015
    Posts:
    40