Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by amer_jib · Oct 14, 2016 at 05:12 PM · texturearrayarrayscondition

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

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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

  1. You have a single renderer

  2. You have the generarate random images every 2 seconds and set the renderer to the generated image

  3. 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];
         }
     }
 }
Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image amer_jib · Oct 17, 2016 at 07:02 PM 0
Share

I have some questions about your final Edit to the code, Is it possible to send private message?

avatar image TBruce amer_jib · Oct 19, 2016 at 06:07 PM 0
Share

What sort of questions?

avatar image amer_jib TBruce · Oct 21, 2016 at 11:03 AM 0
Share

@$$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;"?

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

How to compare a position of an object within an array 1 Answer

[SOLVED] Increase and Decrease size of Class Array in Unity C# 1 Answer

Help! Array index is out of range (C#) 1 Answer

how to not repeat random array 1 Answer

Null reference exception in an if statement that checks for it. 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges