Add object of specific width to a List

I have an unspecified number objects, they all have the same script attached.

Only the width will be different, which I will set in the Inspector.(some will be 0.05f, the others will pe 0.09f ).

How could I Add to a List only the objects with width= 0.05f?
They all need to have the same script.

Thanks for helping!

Hi there @VaderCmp

A simple way to do it would be to hold a reference to the class that contains the list and simply place a conditional in the start method of every object with width that might be added. This way, when the game starts or whenever a width object is instantiated, it will automatically check to see if it can be added to the list and if it can it will add itself to the list.

So for example, it would go like this:

using UnityEngine;
using System.Collections;
using System.Collections.Generic;

// The script attached to every width object.
public class WidthObjectScript : MonoBehaviour 
	// The object's width as defined by the inspector.
	public float ObjectWidth;
	// Reference to the class with the list.
	private TheListClass m_TheListClassRef;

	void Start ()
		// Assign the reference by finding the object with the list class.
		m_TheListClassRef = GameObject.Find ("The List Class Object");

		// Check this object's width.
		CheckWidth ();

	// Check's the width of this object to decide whether it should be added to the list.
	void CheckWidth ()
		// Is the width correct?
		if(ObjectWidth == 0.05f)
			// Add it to the list.
			m_TheListClassRef.WidthObjects.Add (this.gameObject);

// The class containing the list of width objects.
public class TheListClass : MonoBehaviour
	// The list of object's with the correct width.
	public List<GameObject> WidthObjects = new List<GameObject>();

I hope this helps! :slight_smile: