- Home /
Check if var is true from a random object
Seems like I accidently deleted my orginal post and can't recover it ><.
So, I am working on a tic-tac-toe game and I am currently stuck at the last part of the core which is the part that the computer choses a random object (a tile of the platform) to spawn a circle or cross on. But it doesn't know if the tile is already used. In a different script I already have a way to set the tile to used. By checking "var isUsed" to true when hit by a cross or circle.
var circlePrefab : Transform;
var crossPrefab : Transform;
var spawnLocs : GameObject[];
function Update()
{
if (CubeSelectScript.madeMoveCircle && MenuButtonScript.playerVsComputer)
{
MakeMoveCross();
CubeSelectScript.madeMoveCircle = false;
}
else if (CubeSelectScript.madeMoveCross && MenuButtonScript.playerVsComputer)
{
MakeMoveCircle();
CubeSelectScript.madeMoveCross = false;
}
}
function MakeMoveCircle()
{
//Spawn circle on random position
var spawnPos = spawnLocs[(Random.Range(0, spawnLocs.Length))].transform.position;
Instantiate(circlePrefab, spawnPos, Quaternion.identity);
}
function MakeMoveCross()
{
//Spawn cross on random position
var spawnPos = spawnLocs[(Random.Range(0, spawnLocs.Length))].transform.position;
Instantiate(crossPrefab, spawnPos, Quaternion.identity);
}
To Deathcalibur: After compiling with your solution I am getting the following error: "Assets/Standard Assets/Scripts/CubeSelectScript.js(138,10): BCE0089: Type 'CubeSelectScript' already has a definition for 'isUsed'."
CubeSelectScript is the "different script" and line 138 is your "function isUsed()"
The closest way I could get it to is
var spawnPos = Instantiate(circlePrefab, GameObject.FindWithTag("isNotUsed").transform.position, Quaternion.identity);
But it's not random, it picks the objects in the same sequence over and over.
I got it now.
var spawnLocs = GameObject.FindGameObjectsWithTag("isNotUsed");
var spawnPos = spawnLocs[(Random.Range(0, spawnLocs.Length))].transform.position;
Instantiate(circlePrefab, spawnPos, Quaternion.identity);
Thanks for the helps.
How about storing your available spawn positions in an array. Then choose one random element from the array and throw it out afterwards?
Your answer
![](https://koobas.hobune.stream/wayback/20220613092553im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Random time activate/deactivate object 1 Answer
Selecting Random Object and Setting Variable to True 2 Answers
Function action applies to every object that have script with that function on it. Help please 1 Answer
Random time interval 1 Answer
Spawn help, 4 spawners but need one object on screen at a time 1 Answer