Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 gdennis · Mar 03, 2015 at 04:52 PM · prefabontriggerenterwaitforsecondsduplicate

How to avoid accidental OnTriggerEnter

I have several pickup items laying around in my world. This item gameobject has a box collider and a script. I have made this gameobject into a prefab (so i have 3 prefabs usb, cd and shovel). The script attached to it is the following :

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 
 public class PickingItemUp : MonoBehaviour {
     
     public Color color = new Color(0.8f,0.8f,0,1.0f);
     public float scroll = 0.05f;
     public float duration = 1.5f;
     public float alpha;
     public static GameObject guiTextNotif;
     public static bool pickedUp { get; set; }
     public string itemName;
     public bool putInInventory;
     public static bool pickedUpByNPC;
     private Inventory inventory;
 
     GameObject playerObj;
     
     // Use this for initialization
     void Start () {
         playerObj = GameObject.FindGameObjectWithTag ("player");
 
         guiTextNotif = new GameObject ("PickupNotif");
         guiTextNotif.AddComponent<GUIText> ();
         Vector3 pos = new Vector3 (0.5f, 0.5f, 0.0f);
         guiTextNotif.guiText.transform.position = pos;
 
         guiTextNotif.guiText.fontSize = 40;
         guiTextNotif.guiText.anchor = TextAnchor.MiddleCenter;
         guiTextNotif.guiText.alignment = TextAlignment.Center;
 
         pickedUp = false;
         guiTextNotif.GetComponent<GUIText> ().material.color = color;
         alpha = 1.5f;
         putInInventory = false;
         pickedUpByNPC = false;
     }
     
     // Update is called once per frame
     void Update () {
         if (pickedUp) {
             foreach (Transform child in gameObject.transform) {
                 if (child.name == "Item")
                 {
                     Destroy(child.gameObject);
                 }
                 if (child.name == "Particle")
                 {
                     Destroy(child.gameObject);
                 }
             }
             if (alpha > 0) {
                 Vector3 temp = guiTextNotif.transform.position;
                 temp.y += scroll * Time.deltaTime;
                 guiTextNotif.transform.position = temp;
                 alpha -= Time.deltaTime/duration;
                 color = renderer.material.color;
                 color.a = alpha;
                 guiTextNotif.guiText.color = color;
                 renderer.material.color = color;
             } else {
                 pickedUp = false;
                 Destroy (this.gameObject);
                 Destroy (guiTextNotif);
             }
             if (!putInInventory && !pickedUpByNPC) {
                 inventory = GameObject.FindGameObjectWithTag("Inventory").GetComponent<Inventory>();
                 inventory.addItemToInventory(itemName);
                 putInInventory = true;
             }
         }
     }
     
     IEnumerator OnTriggerEnter(Collider other) {
         playerObj.GetComponent<Animation> ().animation.Play ("pickup");
         yield return new WaitForSeconds (2.0f);
         guiTextNotif.guiText.text = "Picked up " + itemName;
         pickedUp = true;
     }
 
     public static void forcePickedUpTrueForNPC(string npcName, GameObject npcObj, string name) {
         guiTextNotif.guiText.text = npcName + " picked up " + name;
         pickedUp = true;
         pickedUpByNPC = true;
     }
     
     public void setItemName(string n) {
         itemName = n;
     }
 }


Now I have 2 main problems: First problem is: if i have 2 of these prefab items (2 USB's) in my world and one gets picked up, the other one will dissapear too. How can I make sure that only the item which is picked up gets removed and the others stay?

Second question is the following: since the characters in my world are not controllable (they move themselves). Sometimes they have to move from one NPC to another NPC. But if they "accidentaly" walk over one of these pickable items (without meaning to pick them up, they were just in their path) the item would be picked up and dissapear. I obviously don't want this to happen. Is there a way where I can put like a small timer on this trigger and say like "if he is after 0.5seconds still on this item he wants to pick it up"?

Thanks in advance to anyone who can help me

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

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by hexagonius · Mar 03, 2015 at 04:59 PM

You should handle all "picking" in the OnTriggerEnter. Check if "other" is who is able to pick this up. Then, do that stuff in OnTriggerEnter. Since OnTriggerEnter is only on the two involved, that's the place where the handling of this event goes. Iterating all "Items" in Update is just a bad idea, because you lost the information on which one it was that was triggered.

Will also solve the problem with NPCs. If an NPC triggers, catch it and do nothing. Keep it simple.

Comment
Add comment · Show 1 · Share
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
avatar image gdennis · Mar 03, 2015 at 06:56 PM 0
Share

Thank you, my first problem got solved! However the second one is still there. $$anonymous$$y "game" is kind of a simulator. It gets fed with 'moves' to perform. These moves can be like this : "player goes to NPC1" or "NPC1 goes to player" or whatever. They can also be "player picks up usb". Imagine the gamemove saying "npc2 goes to npc1", if somewhere along the path that the npc2 takes (everything is with a navmesh & navmeshagents) a random USB is lying on the floor, he will pick it up because he entered the trigger. I hope I am making sense here

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

21 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

Related Questions

Placing Prefabs in Prefabs. 9 Answers

Duplicated Prefabs not behaving as expected 0 Answers

Prefab referenced into Inspector and then Istantiated: Twice in memory? 0 Answers

duplicate a prefab then change the model? 1 Answer

OnCollisionEnter() not getting called between two rigid bodies 3 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