condition for array of textures
hi, I wrote arrays as the type Texture and I assigned them to pictures on the inspector, and I managed to generate them Randomly. I want to make a condition , for example : if GameObject = array [1] ... then print ("u win") if Gameobject = array [2] .. print ("you lost")..
but no matter what I try, it is making all the array items equals , that means this condition will happen for item [1] and [2] ..etc is it possible to separate them somehow? and am I writing the condition correctly?
here is my script :
public int number;
public Texture[] faces = new Texture [11];
public Texture perfectShot;
private Renderer rend;
private bool restart;
private int restartSpeed;
void Start (){
StartCoroutine(WaitBeforeRestart());
restart = false;
InvokeRepeating ("BringingMyRandom", 1, 2);
restartSpeed = 0;
}
void Update (){
if (Input.GetKeyDown(KeyCode.Mouse0))
{
CancelInvoke ("BringingMyRandom");
rend.material.mainTexture = perfectShot;
restart = true;
}
if (restart) {
restartSpeed++;
}
if (restartSpeed >= 100) {
SceneManager.LoadScene ("First Level");
}
}
IEnumerator WaitBeforeRestart () {
yield return new WaitUntil(() => restartSpeed >= 100);
}
void MyMousePress () {
if (Input.GetKeyDown(KeyCode.Mouse0))
{
CancelInvoke ("BringingMyRandom");
rend.material.mainTexture = perfectShot;
restart = true;
}
}
void BringingMyRandom (){
number = Random.Range (0, 11);
number %= faces.Length;
rend = GetComponent<Renderer> ();
rend.material.mainTexture = faces [number];
}
}
Answer by TBruce · Oct 15, 2016 at 09:39 PM
I cleared the previous history on this question and re-evaluated it. As previously stated, I do not know how you have your game set up. But let me assume the following
You have a single renderer
You have the generarate random images every 2 seconds and set the renderer to the generated image
When the player clicks on the image you want to determine if the player clicked a good guy or bad guy and act accordingly
Given that you can do this
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using UnityEngine.SceneManagement;
public class ShootingGallery : MonoBehaviour
{
// this is the value genrated in BringingMyRandom()
// and will be compared against on mouse down
public int currentFaceIndex = -1;
public List<Texture> faces = new List<Texture>();
public Texture perfectShot;
private Renderer rend;
private bool restart;
private int restartSpeed;
void Start ()
{
StartCoroutine(WaitBeforeRestart());
restart = false;
InvokeRepeating ("BringingMyRandom", 1, 2);
restartSpeed = 0;
if (GetComponent<Renderer> () != null)
{
rend = GetComponent<Renderer> ();
}
}
void Update ()
{
if (Input.GetKeyDown(KeyCode.Mouse0))
{
CancelInvoke ("BringingMyRandom");
if (rend != null)
{
rend.material.mainTexture = perfectShot;
}
restart = true;
// if (currentFaceIndex == 9)
if (currentFaceIndex > 9)
{
Debug.Log ("you lost");
}
else
{
Debug.Log ("you win");
}
}
if (restart)
{
restartSpeed++;
}
if (restartSpeed >= 100)
{
SceneManager.LoadScene ("First Level");
}
}
IEnumerator WaitBeforeRestart ()
{
yield return new WaitUntil(() => restartSpeed >= 100);
}
void BringingMyRandom ()
{
if ((faces.Count > 0) && (rend != null))
{
// Random.Range using int will return min to (max - 1)
// so if you have 11 textures the range will be 0 - 10
currentFaceIndex = Random.Range (0, faces.Count);
// currentFaceIndex is the random image selected when the user pressed the mouse button
// this is what we can compare against
rend.material.mainTexture = faces [currentFaceIndex];
}
}
}
I have some questions about your final Edit to the code, Is it possible to send private message?
@$$anonymous$$avina I wanted to ask : why you used List ins$$anonymous$$d of array? what is the difference between them? and what is the purpose of using "System.Collections.Generic;"?