- Home /
How do I repeatedly execute a line of code but its modified by a number?
I am currently on a tic tac toe project and I need to code the draw system. to do that, I need to effectively check whether any of the circles or crosses and place and if so, set the corresponding boolean to true, or at least that is the method I have come up with. Once all are true, A draw boolean in another script is set to true and if there are no wins the game will end. The circles and crosses are numbered to their corresponding position on the grid and will have to be reached from another script named Variables.
here is my messed up rubbish script
using System.Collections; using System.Collections.Generic; using UnityEngine; public class Draw : MonoBehaviour { bool GridSpace_1; bool GridSpace_2; bool GridSpace_3; bool GridSpace_4; bool GridSpace_5; bool GridSpace_6; bool GridSpace_7; bool GridSpace_8; bool GridSpace_9; Variables Var; void Start() { Var = GetComponent<Variables>(); } void Update() { for (int i = 9; i > 0; --i) { if (Var.Circle_[i].transform.position.y != 10) { //WHY IS THERE AN ERROR } } } }
P.S. IT DOESNT WORK
Answer by Ian_coding · Dec 18, 2020 at 08:37 AM
I realised this might be confusing but by checking if the circles are not at the 10th y position, im checking if they are used
Answer by Llama_w_2Ls · Dec 20, 2020 at 08:20 PM
This section here:
for (int i = 9; i > 0; --i)
{
if (Var.Circle_[i].transform.position.y != 10)
{
}
}
Assumes that you have 10 elements in the public array Circle_
in your Var script. Which you probably don't or shouldn't have, since there's only 9 squares in a standard game of tic-tac-toe.
Instead, it should be:
for (int i < 9; i >= 0; --i)
{
if (Var.Circle_[i].transform.position.y != 10)
{
}
}
Note: Remember that array indexing starts at 0, so myArray[0]
is the FIRST element in the array and myArray[9]
is the TENTH element in the array.
This should solve your 'error'. I did successfully manage to create a tic-tac-toe game, with win-checking and two player etc. but not drawing, since I did it in the Visual Studio console, in a console project, and not in Unity. I can help you further if you wish. @Ian_coding
Your answer
![](https://koobas.hobune.stream/wayback/20220613021831im_/https://answers.unity.com/themes/thub/images/avi.jpg)