public GameObject[] items;
public GameObject pickup;
void Update(){
if(PickedUpItem()){
Debug.Log("Yay!");
}
}
public bool PickedUpItem(){
foreach(GameObject i in items){
if (pickup.GetComponent <Collider2D>().IsTouching (i.GetComponent <Collider2D>())){
return true;
}
}
return false;
}
what suppose to happen is when i touch one of the gameobjects in items, im gonna return true in the PickupUpItem(). But it wont return true (say “Yay”) even though i’ve touched one of the items. What could be causing the problem?
I’m not sure why you are checking in update if you’ve picked up an item. You can use something like OnTriggerEnter or OnCollisionEnter or something much better to trigger a “Hey I’ve been picked up” instead of asking every pickup if it’s been touched.
The other thing is, maybe when you’re touching a physics update hasnt’ taken place.
I’m not sure why making a plugin would be any different.
If you have a character that is picking up stuff, a player script on that character would be better with collision or trigger checks.
It’s not a good way to do it, and I would pass on an asset like this. It’d be easy to have people setup their pickups with a tag to make it a pickup item, that way you just check the objects tag with OnTriggerEnter or OnCollisionEnter.
Even if you don’t want to do that, if you have a list of pickup items and you just want to check if that list contains an item the player char collides with, you’d still trigger that check in OnTriggerEnter or OnCollisionEnter.
Either method is better than looping through an array multiple times through an update call.
Think of it this way. If you were in a building full of 100 people and I told you to go ask each of them if they were hungry and you had to do this over and over and over…
or, you could wait for a person to come to you and tell you they are hungry. Which is better?