0

enter image description here I have two GameObject arrays with same length. I am trying to copy same values of array1 to array2. I tried using system.Array.copy(Array1,Array2,4) and also tried Array1= Array2 but not working. These two arrays are holding 4 buttons each with child text. I want show 4 answers that are assigned to these buttons and copy same answers to another 4 buttons at the same time. Something like dual player. Can anyone please help?

public class DuelMode : MonoBehaviour{
public static DuelMode instance;

// these are the question values a and b
private int a, b, a1, b1;

//the variable for answer value
[HideInInspector] public int answer;

//varible whihc will assign ans to any one of the 4 answer button
private int locationOfAnswer;

//ref to the button
public GameObject[] ansButtons;
private GameObject[] ansButtonsDuel;

//ref to image symbol so player can know which operation is to be done
public Image mathSymbolObject;

//ref to all the symbol sprites whihc will be used in above image
public Sprite[] mathSymbols;

//get the tag of button 
public string tagOfButton;

//ref to text in scene where we will assign a and b values of question
public Text valueA, valueB, valueA1, valueB1;
void Awake()
{
    MakeInstance();
}

//method whihc make this object instance
void MakeInstance()
{
    if (instance == null)
    {
        instance = this;
    }
}

//at start we need to do few basic setups
void Start()
{
    //we put the location value in tag of button variable
    tagOfButton = locationOfAnswer.ToString();

    MathsProblem();
}

//this method keeps the track of mode 

// Update is called once per frame
void Update()
{
    tagOfButton = locationOfAnswer.ToString();

}


//Below code is for maths calculation

public void MathsProblem()
{
    bool roundActive = true;

}


//this methode perform Multiplication process
void MultiplicationMethod()
{
    bool reloop;
    bool[] numbers = new bool[301];

    b = b1 = Random.Range(1, 10);

    locationOfAnswer = Random.Range(0, ansButtons.Length);


    answer = a * b;
    numbers[answer] = true;

    if (valueA != null && valueB != null && valueA1 != null && valueB1 != null)
    {
        valueA.text = a.ToString();
        valueB.text = b.ToString();
        valueA1.text = a.ToString();
        valueB1.text = b.ToString();
    }
    mathSymbolObject.sprite = mathSymbols[0];

    for (int i = 0; i < ansButtons.Length; i++)
    {
        if (i == locationOfAnswer)
        {
            ansButtons[i].GetComponentInChildren<Text>().text = "" + answer;

        }
        else
        {
            // the below code make sure that all the values assigned to the ans button are within the range

            if (a * b <= 100)
            {
                ansButtons[i].GetComponentInChildren<Text>().text = "" + Random.Range(1, 101);
            }
            else if (a * b <= 200 & a * b > 100)
            {
                ansButtons[i].GetComponentInChildren<Text>().text = "" + Random.Range(101, 201);
            }
            else if (a * b <= 300 & a * b > 200)
            {
                ansButtons[i].GetComponentInChildren<Text>().text = "" + Random.Range(201, 301);
            }
            else if (a * b <= 400 & a * b > 300)
            {
                ansButtons[i].GetComponentInChildren<Text>().text = "" + Random.Range(301, 401);
            }

            while (ansButtons[i].GetComponentInChildren<Text>().text == "" + answer)
            {
                ansButtons[i].GetComponentInChildren<Text>().text = "" + Random.Range(1, 401);
            }
        }

    }

}

}

8
  • 1
    Can you post more of your code so you can be offered assistance Commented Jan 16, 2019 at 11:19
  • 1
    Have you looked at Object.Instantiate()? You should be able to use that in a loop to clone each element into a new array. Commented Jan 16, 2019 at 11:22
  • Those arrays only hold references to the GameObjects ... so copiing the references into a new array doesn't help much in order to have more GameObjects ... see above Commented Jan 16, 2019 at 11:29
  • yes updating my code Commented Jan 16, 2019 at 11:33
  • @Sarita could you brake that down to the esential parts? Also I don't see where you are trying to copy/clone the array Commented Jan 16, 2019 at 11:40

2 Answers 2

1

I assume when you are finished setting texts for ansButtons now you simply want to display the same texts on both sides, right?

for(int i = 0; i < ansButtons; i++)
{
    // get the GameObjects
    var ansButton = ansButtons[i];
    var ansDuelButton = ansButtonsDuel[i];

    // get the Text components 
    var ansText = ansButton.GetComponentInChildren<Text>(true);
    var duelText = ansDuelButton .GetComponentInChildren<Text>(true);

    // copy the text over
    duelText.text = ansText.text;
}

I asume ofcourse you have all the references in ansButtons and ansButtonsDuel setup e.g. via the inspector or while Instantiate the objects.

Sign up to request clarification or add additional context in comments.

9 Comments

ok so i dont have to actually copy array elements. just need to assign to another array.
well I asume ofcourse you have all the references in ansButtons and ansButtonsDuel setup e.g. via the inspector or while spawning (Instantiate) the objects. And than yes
now i am able to get numbers on another 4 buttons but they are not the same as ansButtons values. i mean i am getting 8 different values instead of 4
I don't see where those should come from since you copy the final numbers as string ... You have to put the for loop above after the for loop you already had so right after setting all texts to ansButtons
yes i did exactly the same. entered this new for loop above mine.
|
-1

Please can you post example of your code and the object array. This will help us understand the problem so we can then resolve the issue.

You can also have a look at the post below. It may help your cause.

Difference Between Array.CopyTo() and Array.CloneTo()

EDIT:

    var foo = new String[] {"ab" , "cd", "ef"} ;
    var bar = new List<string>();

    foreach(var item in foo)
    {
        bar.Add(item);
    }
    var barArray = bar.ToArray();

    for(int i = 0; i < foo.Length; i++)
    {
        Console.WriteLine(foo[i]);
        Console.WriteLine(barArray[i]);
    }

The code above makes use of list and arrays since that way you will not have to index your duplicate array. You can run the code in dot net fiddle to see the output. I have used string in this example, please replace with your object.

4 Comments

The underlying issue is that a GameObject[] contains only references to existing GameObjects .. the question of OP sounds like he wants to actually copy/clone the GameObjects not only the references
If this is the case then the OP will have to loop through and add each item from Array 1 to a new array Array2. We ideally need an example of their code and expected outcome.
Yes and that's why your post should not be an answer but rather a comment (you will have to wait until you have enough reputation to post comments on the question)
I was just updating the answer guys, hold your horses

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.