1

How can I store gameobjects/images in an array so that I can just use the "for loop" in changing the color of the image instead of individual changing its color.. I'm using c# in unity3d..

my code:

public Image clay, sand, loam, silt, siltyClay, siltyLoam, loamySand,   sandyLoam, siltyClayLoam;

public void imageColor(){
        sand.color = Color.green;
        clay.color = Color.white;
        loam.color = Color.white;
        silt.color = Color.white;
        siltyClay.color = Color.white;
        siltyLoam.color = Color.white;
        loamySand.color = Color.white;
        sandyLoam.color = Color.white;
        siltyClayLoam.color = Color.white;
        mountTex = (Texture)Resources.Load("sand", typeof(Texture));
}
5
  • What is relationship between color and Image? only first is green and other are white, isn't it? Commented Apr 12, 2016 at 12:33
  • its just that the images' color will change to green when it is picked.. Commented Apr 12, 2016 at 12:47
  • note, as a rule never use arrays in Unity. Use List<>. Commented Apr 12, 2016 at 13:07
  • Is this meant to be some exclusive selection like thing? Like the one you pick gets green and all the others white and if you click another one, that one gets green and the others get/stay white? Commented Apr 12, 2016 at 13:14
  • @GunnarB. - yes, I believe it's that simple. OP just doesn't know the basic trick of "all off / one on". (OP incorrectly thinks should use a for structure of some type, and is thus asking how to do that.) Commented Apr 12, 2016 at 13:18

2 Answers 2

5

Funnily enough, the best way to do it, is how you already have it.

There's only one thing you're not getting:

when you want to "highlight one thing", the programming patter is turn them all off, and then turn just the one on.

It's a basic programming trick:

public Image clay, sand, loam, silt, siltyClay,
   siltyLoam, loamySand, sandyLoam, siltyClayLoam;

private void AllOff()
    {
    sand.color = Color.white;
    clay.color = Color.white;
    loam.color = Color.white;
    silt.color = Color.white;
    siltyClay.color = Color.white;
    siltyLoam.color = Color.white;
    loamySand.color = Color.white;
    sandyLoam.color = Color.white;
    siltyClayLoam.color = Color.white;
    }

public Highlight( Image x )
    {
    AllOff();
    x.color = Color.green;
    }

to use ..

Highlight( silt );

The nine items in question are not, really, a list - so don't use them that way.

Note that in other cases you should do this:

  public Image[] images;

try it and see, in the Inspector you can just drag them in.

To work on them all at once, it's just

foreach (Image i in images)
  Debug.Log(i);
Sign up to request clarification or add additional context in comments.

1 Comment

right, there should be a book "the 25 most ingenious software engineering tricks". Even the most gifted natural programmers don't think of that pattern on their own, someone has to point it out.
1

You seem to want different colors for different images. You can e.g. group the Images and colors together.

public Image clay, sand, loam, silt, siltyClay, siltyLoam, loamySand, sandyLoam, siltyClayLoam;

[System.Serializable]
public class ImageColorPairs
{
    public Image[] images {get;set; } /* associate Images */
    public Color targetColor { get; set; } /* ..with a target color */
}

private ImageColorPairs[] pairs; //Declare here but initialize later. Can't initialize these objects (error: referencing non-static field..)

public void InitializePairsOnce()
{
    /* Make up array of Pairs here */
    pairs = new ImageColorPairs[] {
        new ImageColorPairs() { images = new Image[] { sand }, targetColor = Color.green } ,
        new ImageColorPairs() { images = new Image[] { clay, loam, silt, siltyClay, siltyLoam, loamySand, sandyLoam, siltyClayLoam }, targetColor = Color.white }
 };
}

public void ImageColor()
{
    /* Now setting the colors is easy! :) */
    foreach (var pair in pairs)
    {
        foreach (var image in pair.images)
        {
            image.color = pair.targetColor;
        }
    }
}

You can also make it use more basic data strucutres like a your own ImageTuple, which would then associate each image individually a color. But up there you seem to only have two basic groups, so it's less work to just associate a color for a whole array of images.

With such a class you would do it like

[System.Serializable]
public class ImageTuple
{
    public Image image;
    public Color targetColor;

    public ImageTuple(Image img, Color col)
    {
        image = img;
        targetColor = col;
    }
}
private ImageTuple[] imageTuples; 

public void InitializeTuplesOnce()
{
    imageTuples = new ImageTuple[]
    {
        new ImageTuple(clay, Color.green),
        new ImageTuple(sand, Color.white),
        //.. the rest
    };
}

public void ImageColor()
{
    foreach (var pair in imageTuples)
    {
        pair.image.color = pair.targetColor;
    }
}

3 Comments

I tried your first code but I received an error saying " Cannot initialize object of type `UnityEngine.UI.Image[]' with a collection initializer".. help..
i think it's missing a new()
Also I changed from a property to a field, so brace initialization broke, sorry. Correcting.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.