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);
            }
        }

    }

}

}

Sarita
  • 51
  • 3
  • 8
  • 1
    Can you post more of your code so you can be offered assistance – preciousbetine Jan 16 '19 at 11:19
  • 1
    Have you looked at [`Object.Instantiate()`](https://docs.unity3d.com/ScriptReference/Object.Instantiate.html?from=MonoBehaviour)? You should be able to use that in a loop to clone each element into a new array. – Matthew Watson Jan 16 '19 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 – derHugo Jan 16 '19 at 11:29
  • yes updating my code – Sarita Jan 16 '19 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 – derHugo Jan 16 '19 at 11:40
  • i have updated my code. I have assigned 4 answers to the array ansButtons . now I WANT TO COPY SAME ANSWERS TO ARRAY ansButtonsDuel . sorry i deleted because it didnt work. – Sarita Jan 16 '19 at 11:43
  • i tried System.Array(ansButtons.AnsButtonsDuel,4) in update function so every time new question is created it will copy array elements. – Sarita Jan 16 '19 at 11:44
  • Possible duplicate of [Copy Arrays to Array](https://stackoverflow.com/questions/46070530/copy-arrays-to-array) – Remy Jan 16 '19 at 12:03

2 Answers2

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.

derHugo
  • 83,094
  • 9
  • 75
  • 115
  • ok so i dont have to actually copy array elements. just need to assign to another array. – Sarita Jan 16 '19 at 12:08
  • 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 – derHugo Jan 16 '19 at 12:11
  • 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 – Sarita Jan 16 '19 at 12:23
  • 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` – derHugo Jan 16 '19 at 12:28
  • yes i did exactly the same. entered this new for loop above mine. – Sarita Jan 16 '19 at 12:30
  • I hope not `above` but **below** – derHugo Jan 16 '19 at 12:31
  • oh ! i moved it below the existing for loop. and now it works perfectly fine. thanks a ton sir for your support – Sarita Jan 16 '19 at 12:35
  • @Sarita Glad to help! Sorry I guess I formulated it strange in `You have to put the for loop above after the for loop you already had` ... with `above` there I refered to the code of my answer – derHugo Jan 16 '19 at 12:37
  • that is absolutely fine. my bad too – Sarita Jan 16 '19 at 12:39
-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.

Mahan.A
  • 79
  • 1
  • 7
  • The underlying issue is that a `GameObject[]` contains only references to existing `GameObject`s .. the question of OP sounds like he wants to actually copy/clone the GameObjects not only the references – derHugo Jan 16 '19 at 11:33
  • 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. – Mahan.A Jan 16 '19 at 11:36
  • 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) – derHugo Jan 16 '19 at 11:37
  • I was just updating the answer guys, hold your horses – Mahan.A Jan 16 '19 at 12:11