0

How to convert the variable below to become Multi Array Variable ?

The code example :

public GameObject Mayonnaise;
    Text Qty;
    Text Price;
    Text Have;

    public GameObject Cheese;
    Text Qty;
    Text Price;
    Text Have;

    public GameObject Flour;
    Text Qty;
    Text Price;
    Text Have;

    public GameObject Sugar;
    Text Qty;
    Text Price;
    Text Have;

I want that gameobject Mayonnaise contain Qty, Price, Have;

So it will look like : Mayonnaise["qty","Price","Have"]

So I no need to create too many variable name with the same one.

How is it in Unity C# ?

Thanks

3
  • You can make an array of GameObjects like this. public GameObjec[] sampleObjets but you if i underesntad your question correclty you try to make array of gameobjects of Texts. You can also check Dictionary here is more info: link. Commented Jul 12, 2016 at 10:04
  • Could you make the example convert the code above ? Commented Jul 12, 2016 at 10:23
  • First i need to know why you want construction like this. Why did you create e.g public GameObject Mayonnaise and than Text Qty ,Text Price? Maybe you should consider to create class. E.g public class Product { public Text Qty; public Text Price; public Text Have; } and than create objects e.g: public Product Mayonnaise = new Product(); Mayonnaise.Qty = //your text component etc. link Commented Jul 12, 2016 at 10:32

1 Answer 1

2

I want that gameobject Mayonnaise contain Qty, Price, Have;

A game Object cannot contain those. It looks like you are looking for a way to hold those those variables and using scripts to do this is appropriate.

So I no need to create too many variable name with the same one

All you need is a script that holds each food type. You can then create arrays of each food type. This is what this question sounds to me and hopefully, I read this correctly.

If you look very close in your question, you will see that Qty,Price, and Have variables appeared in all scripts. This is where inheritance should be used.

Food class:

public class Food
{
    protected Text Qty;
    protected Text Price;
    protected Text Have;

    public void setQty(Text Qty)
    {
        this.Qty = Qty;
    }

    public void setPrice(Text Price)
    {
        this.Price = Price;
    }

    public void setHave(Text Have)
    {
        this.Have = Have;
    }

    public Text getQty()
    {
        return this.Qty;
    }

    public Text getPrice()
    {
        return this.Price;
    }

    public Text getHave()
    {
        return this.Have;
    }
}

You can then Create your Mayonnaise,Cheese,Flour and Sugar classes and inherit from Food on all of them.

public class Mayonnaise : Food
{

}

public class Cheese : Food
{

}

public class Flour : Food
{

}

public class Sugar : Food
{

}

Now, to answer your question about creating multiple arrays of them, below is like what you are looking for.

const int Size = 4;

Mayonnaise[] mayonnaise;
Cheese[] cheese;
Flour[] flour;
Sugar[] sugar;

void Start()
{
    mayonnaise = new Mayonnaise[Size];
    cheese = new Cheese[Size];
    flour = new Flour[Size];
    sugar = new Sugar[Size];

    //Create instance of of them
    for (int i = 0; i < Size; i++)
    {
        mayonnaise[i] = new Mayonnaise();
        cheese[i] = new Cheese();
        flour[i] = new Flour();
        sugar[i] = new Sugar();
    }

    mayonnaise[0].setHave(SomeTextComponent);
    mayonnaise[0].getHave().text = "Some Text";
    string text = mayonnaise[0].getHave().text;
}
Sign up to request clarification or add additional context in comments.

Comments

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.