- Home /
How can i detect how many objects of the same type i'm triggering and choose only one of them?
Hi, i'm italian and my english is not good so... sorry! I'm developing a 2D game. In this game i can control a lumberman, so I can cut trees and move the wood wherever I want. if I do not have wood on me and the trigger detect the wood on floor, when i press "E", he takes the wood. The problem comes when i'm triggering 2 pieces of wood! In this case he collect both of them but, of course, then i have only 1 wood on the lumberman. I need to write a code that recognizes when I'm touching more than one object of the same type and, by pressing the "E" key, collect only one of these. Can someone help me? Thank you
do you have some triggerzone? or how can you get in the colliders area? i’d recomend you to use a raycast and detect the gameobjects
Not this exactly But Something kinda along these lines:
using UnityEngine;
using System.Collections.Generic;
public class HoriDoor$$anonymous$$anager : $$anonymous$$onoBehaviour
{
public DoorHori door1;
public DoorHori door2;
public bool isDoorClosed;
const int DoorCount$$anonymous$$ey=12;
public List<HoriDoor$$anonymous$$anager>$$anonymous$$anagerList=new List<HoriDoor$$anonymous$$anager>(DoorCount$$anonymous$$ey);
public HoriDoor$$anonymous$$anager(){
}
public HoriDoor$$anonymous$$anager(DoorHori d1,DoorHori d2){
HoriDoor$$anonymous$$anager new$$anonymous$$anger = new HoriDoor$$anonymous$$anager();
new$$anonymous$$anger.door1=d1;
new$$anonymous$$anger.door2=d2;
new$$anonymous$$anger.isDoorClosed=true;
$$anonymous$$anagerList.Add(new$$anonymous$$anger);
}
public HoriDoor$$anonymous$$anager(DoorHori d1,DoorHori d2,bool locked){
HoriDoor$$anonymous$$anager new$$anonymous$$anger = new HoriDoor$$anonymous$$anager();
new$$anonymous$$anger.door1=d1;
new$$anonymous$$anger.door2=d2;
new$$anonymous$$anger.isDoorClosed=true;
$$anonymous$$anagerList.Add(new$$anonymous$$anger);
}
void OnTriggerEnter()
{
if (isDoorClosed == false)
{
if (door1 != null)
{
door1.OpenDoor();
}
if (door2 != null)
{
door2.OpenDoor();
}
}
}
}
using UnityEngine;
using System.Collections;
public class DoorHori : $$anonymous$$onoBehaviour {
public float translateValue;
public float easeTime;
public OTween.EaseType ease;
public float waitTime;
private Vector3 StartlocalPos;
private Vector3 endlocalPos;
private void Start(){
StartlocalPos = transform.localPosition;
gameObject.isStatic = false;
}
public void OpenDoor(){
OTween.ValueTo( gameObject,ease,0.0f,-translateValue,easeTime,0.0f,"StartOpen","UpdateOpenDoor","EndOpen");
GetComponent<AudioSource>().Play();
}
private void UpdateOpenDoor(float f){
Vector3 pos = transform.TransformDirection( new Vector3( 1,0,0));
transform.localPosition = StartlocalPos + pos*f;
}
private void UpdateCloseDoor(float f){
Vector3 pos = transform.TransformDirection( new Vector3( -f,0,0)) ;
transform.localPosition = endlocalPos-pos;
}
private void EndOpen(){
endlocalPos = transform.localPosition ;
StartCoroutine( WaitToClose());
}
private IEnumerator WaitToClose(){
yield return new WaitForSeconds(waitTime);
OTween.ValueTo( gameObject,ease,0.0f,translateValue,easeTime,0.0f,"StartClose","UpdateCloseDoor","EndClose");
GetComponent<AudioSource>().Play();
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class DoorsLock$$anonymous$$anager : $$anonymous$$onoBehaviour
{
public bool lockStateRealTime = false;
public Renderer rend;
//Realized by looking at it that each manager deals with 1 door and has 2 for: A = unlocked, B = locked.
public List<HoriDoor$$anonymous$$anager> door$$anonymous$$anagers=new List<HoriDoor$$anonymous$$anager>();
private Shader unlitcolor;
private List<GameObject> doorPlanes=new List<GameObject> ();
private void Start()
{
doorPlanes = GameObject.FindGameObjectsWithTag("DoorPlane");
ChangeColors(new Color32(255, 0, 0, 255), new Color32(0, 255, 0, 255));
}
private void Change$$anonymous$$aterialSettings()
{
unlitcolor = Shader.Find("Unlit/Color");
rend.material.shader = unlitcolor;
rend.material.SetFloat("_$$anonymous$$etallic", 1);
}
private void ChangeColors(Color32 lockedColor, Color32 unlockedColor)
{
for (int i = 0; i < doorPlanes.Length; i++)
{
rend = doorPlanes[i].GetComponent<Renderer>();
Change$$anonymous$$aterialSettings();
if (door$$anonymous$$anager.isDoorClosed == true)
{
rend.material.color = lockedColor;
}
else
{
rend.material.color = unlockedColor;
}
}
}
// Update is called once per frame
void Update()
{
lockStateRealTime = door$$anonymous$$anager.isDoorClosed;
if (lockStateRealTime)
{
ChangeColors(new Color32(255, 0, 0, 255), new Color32(0, 255, 0, 255));
}
}
}
Also some of this is wrong... currently in the process of helpin someone
Answer by xxmariofer · Feb 21, 2019 at 02:53 PM
depends on how you plan to do it. Unity triggers events are only triggered by 2 colliders, so even if your lumberman is triggering multiple colliders, you wont receive one trigger message with all the colliders, but multiple trigger messages (one per collider in trigger). i think you dont really need to know when you are touching more than 1 object, since you can just pick up the first object and set a bool variable to hasTakenWood = true. and only take the wood if that var is set to false. if for some reason you need to know how many woods you are colliding with use the ontriggerstay for adding each collider of type wood he is getting trigger with and add it to a list. and in the fixedupdate clear that list.
I started realizing that also aver a suggestive comment, unless I $$anonymous$$now an answer for certain, not to post as an answer. Comment, and let others set it as an Answer, also try to reply to the specific comment in particular. Just thought I'd say so nicely ;)
I asked why they didn't set their Clearly an Answer as a comment, and he said "well unless i get an upvote of a confirm i don't know it it is the answer to the question." I was like to myself Ahhhhh makes soo much more sense now.....
i know that both of this methods would work for his issues thats why i post it as an answer, i think i answered his topic, he explained his issue, and asked for a solution, i only told him he could do it other way and told him how to do it in the way he was asking. if you think my solution isnt clear/good please give it a down vote, since is how the forum works, and when the answer is correct give it an upvote. for basic questions like triggers i dont usually post them as a comment because i know it works, but if you think is not a valid answer please down vote it.
Your answer
![](https://koobas.hobune.stream/wayback/20220612192155im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
How to make a little wiggle room for the collision? 1 Answer
2D Tilemaps: how to set colliders on scriptable tiles derived from Tilebase class 1 Answer
A node in a childnode? 1 Answer
How can I allow the rope to correctly interact with Box and Polygon colliders? 0 Answers
Different platform effector 2d rotations for different colliders 1 Answer