How can I eliminate searching for a struct corresponding to the collider returned by OnCollisionEnter()

I have a player struct with a gameobject, transform, necessary values, and a weapon. I want to call OnCollisionEnter for my weapon from my player.

I am trying to think out how to detect a collision. I want to get the values from the player, the weapon, and the effected enemy without running a function that searches through names or something to find a match.

I’m not sure how abstract this is, feel free to ask questions

You should learn how to use EventArgs. Fire an event in OnCollisionEnter, which contains the data you need. Subscribe to the event where necessary, and use the data as you see fit.

It’s certainly possible to pass that collision event to another script – use GetComponent to find the other script, write a function which takes the data in, then call that function from the original object’s OnCollisionEnter.

For example, I once worked on a game where the player had multiple colliders: incoming projectiles could hit their shield, or their body. I had a simple component attached to each child collider, to detect the collisions, which could then pass them up to the main player script for processing.

Here’s another UA thread which discusses messages and calls between scripts.

I would strongly recommend against generating your own collision data. Much easier to delegate.

So I couldn’t change some variables in my structs from like a {get;set;} pointer like thing… ya know that thing. Anyways I went looking around and found someone with that same problem. The solution was to change the structs into classes.

I changed over and painfully struggled to get it working until I found out that I had to set each class variable in an array to a new class object. Like I have two legs right.

pl.leg=new limb[2]; //Right?

so I had to go through each one and declare it again

pl.leg[0]=new limb();

pl.leg1=new limb();

I guess it was simple enough, with larger arrays its easy to just use a FOR statement.
I had to do extra checks to make sure thing were initialized to like if(Owner!=null) aswell.

Anyway I got it all going, thanks meat, rutter, jessy, turkeybag!

alt text