How to get an object in a list to have a number assigned to it based on list position

Hello, I am trying to make a system where my character changes collision boxes based on what animation is playing. I am doing this by giving the player character “collision” child objects. Each child object has a single, uniquely sized collision box, and a “ChildColliderController” script which will hold a number, and set a collision box to “isCurrentCollisionBox”. The parent object, “Player”, has a list called “Child Collider List”, which all the child object’s BoxColliders are stored.

Now, the problem: I want to be able to pass a number to each instance of ChildCollider based on their respective position in the list. I’m not sure how to do this.

The next problem (not relevant till the first one is figured out, but might as well post it): I want the child objects to be able to pass their number into the parent object based on the bool isCurrentCollisionBox. I think this is the same problem (essentially) as the first one, but in case it’s not, here it is.

This is the code in the parent

public List<BoxCollider2D> childColliderList;
int activeCollider=0;

public void ChangeCollisionBox()
    collider = childColliderList[activeCollider];

This is the code in the child

bool isActive=false;
public bool listNumber;

There isn’t anything in there that actually does anything yet.
The point of all this is so I can work with many different children and not have to manually enter an ID of a sort for each one.

Any help would be appreciated, Thanks in advance!

To do just what you asked for, you could use list.IndexOf(value):

I probably don’t understand the question, but I think you will be better off if in the parent you store List<ChildColliderController> childColliderList and set from the parent ChildColliderController.listNumber by looping through the list:

// do this whenever you modify the list
for (int i = 0; i < childColliderList.Count; ++i)
    childColliderList.listNumber = i;

Was this your question, and did this answer it?

Kinda. I actually found a different solution. I came back here to update this and say “hey I found something simpler”. Thank you for your response though, I didn’t think of a loop, although that was what I was looking for. I’m not done coding my new solution, but it’ll work better than passing a value from a list to a child then back to the list. What I am doing now is having a method that takes two Vector2 vars and puts one in the Collider.size, and the other in Collider.transform.position. The child would call the method whenever the conditions are met for a new BoxCollider I’ll post the code here when I get it working. Or I’ll post another question if I get stumped. :slight_smile:


I put this in the comments before realizing I should put it here. This is a Copy-Past of the first comment.

"Here is what I finally did (note this is a “proof-of-concept” code, and it is not as fleshed out as it will be when fully implemented).

This is the code inside the Parent

public BoxCollider2D collider;

public void ChangeCollisionBox(Vector2 newSize, Vector2 newOffset)
   collider.size = newSize;
   collider.offset = newOffset;

This is the code inside the Child

private string iD;
private Vector2 colliderSize;
private Vector2 colliderOffset;
private void Awake()
   colliderSize = GetComponent<BoxCollider2D>().size;
   colliderOffset = GetComponent<BoxCollider2D>().offset;
private void Update()
   if(Input.GetAxisRaw("Horizontal")==0 && Input.GetAxisRaw("Vertical")==0)
   else { IsAccelerating(); }
void IsIdle()
       GetComponentInParent<RaycastController>().ChangeCollisionBox(colliderSize, colliderOffset);
void IsAccelerating()
   if (iD == "Accelerate")
       GetComponentInParent<RaycastController>().ChangeCollisionBox(colliderSize, colliderOffset);