Hey guys,
my script works in level 01 but it doesnt work in 02. am creating a fairly simple game where the player simply clicks a bunch of squares trying to find the correctly colored square to proceed to the next level. If they get the wrong square, the level will restart.
Level 01:
Everything functions perfectly. The level begins and the Win square and Lose square are randomly selected and assigned. I click the red square and my level restarts as it should. I click the green square and it moves me to level 02.
Level 02:
After 1 second, 5 prefab balls drop from the sky. A few seconds later, the same event happens. after a set timer, the selector function is called and the balls are assigned their correct roles. Everything appears to be functioning properly. That is, until i actually try to play the level…
The balls drop according to the timer. The roles are selected according to the timer. the only problem is, is when i click the balls, nothing happens. it is literally the exact some code (Meaning i assigned the SAME code file to BOTH objects. )
ALSO:
i only want one green and one red object per level, but as it stands now, i cant seem to get my code to only do one… its either all of them, or adding one at a time according to the timer (sets 2 then 10 seconds later set an additional 2)
Id also like to make the non win/lose squares a different color but i cant seem to get the code right. I’ll explain a little more once i have the codes posted…
BLOCKSCRIPT:
public class blockScript : MonoBehaviour {
public static int gameScore;
public Text Score;
void Start () {
gameScore = 0;
Score.text = "Score: 0";
}
void OnMouseDown () {
Debug.Log ("WORKING");
gameObject.SetActive (false);
IncreaseScore ();
//this doesnt happen...
}
public void IncreaseScore (){
gameScore += 1;
Debug.Log (gameScore);
Score.text = "Score: " + gameScore.ToString();
}
}
FINDER SCRIPT (Selects the win/lose)
(in level 01 i have TWO DIFFERENT OBJECTS doing this)
public class finder : MonoBehaviour {
private blockScript [] allBlocks;
public float timer;
void Start () {
timer = 3.5f;
//how long till the win/lose is selected
}
void Update () {
timer -= Time.deltaTime;
if (timer < 0) {
Selector ();
timer = 10;
//This is what i mean by additional 2
}
}
void Selector () {
allBlocks = FindObjectsOfType<blockScript> ();
blockScript selectedBlock = allBlocks[Random.Range(0, allBlocks.Length)];
blockScript selectedBlock2 = allBlocks[Random.Range(0, allBlocks.Length)];
GameObject selectedGO = selectedBlock.gameObject;
GameObject selectedGO2 = selectedBlock2.gameObject;
selectedBlock.enabled =false;
selectedBlock2.enabled =false;
redBlock block = selectedGO.AddComponent<redBlock> ();
block.ChangeColor();
winBlock block2 = selectedGO2.AddComponent<winBlock> ();
block2.ChangeColor();
}
}
ColorChanger:
public class colorChanger : MonoBehaviour {
public Color green = Color.green;
public Color blue = Color.blue;
public Color magenta = Color.magenta;
public Color white = Color.white;
public Color yellow = Color.yellow;
public Renderer rend;
public int color;
void Start () {
color = Random.Range (0, 4);
Debug.Log ("it's running");
rend.material.color = blue;
if (color >= 0) {
rend.material.color = green;
}
else
if (color >= 1) {
rend.material.color = blue;
}else
if (color >= 2) {
rend.material.color = magenta;
}
else
if (color >= 3) {
rend.material.color = white;
}
else
if (color >= 4) {
rend.material.color = yellow;
}
}
public void ColorChanger () {
Debug.Log ("ColorChanger");
}
}
Let me know if you need more information.
Thank you all in advance for any and all of your help!