Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

How to "talk" to the objects?

Discussion in 'Scripting' started by HrC123, Feb 3, 2011.

  1. HrC123

    HrC123

    Joined:
    Feb 2, 2011
    Posts:
    140
    hi

    I want to make 1024 White buttons, when I click one of them it turns red, so I need to make 1024 button texture variables (or 1 arrayed variable with 1024 indexes) but this doesn't work:

    Code (csharp):
    1. Texture[] btnT = new int[1024];
    it says that it can't convert int[] to texture[]
    but isn't that int the number of indexes?
    I mean btnT[1], btnT[2], ... ,btnT[1024]
     
    Last edited: Feb 3, 2011
  2. Jesse Anders

    Jesse Anders

    Joined:
    Apr 5, 2008
    Posts:
    2,857
    Where you have 'int', that should be the element type for the array. (You want an element type of 'Texture'.)
     
  3. ciaravoh

    ciaravoh

    Joined:
    Dec 16, 2009
    Posts:
    252
    ok, let's try this.

    you can declare public var and you drop the texture you want for each states (active and inactive for example).

    Code (csharp):
    1. public var TextureInactive : Texture2D;
    2. public var TextureActive : Texture2D;
    Then in your code, when you touch the object

    you have to wrap this code into your "TOUCH" script (touch, etc, etc)....
    Code (csharp):
    1.  
    2. .....
    3. .....
    4. var ray = touchCamera.ScreenPointToRay (touch.position);
    5. if (Physics.Raycast (ray, hit, 15000))
    6. {
    7.      if(hit.transform.tag == "YOUR-OBJECT-TAG"  touchCount == 1)
    8.       {
    9.     hit.transform.renderer.material.mainTexture = TextureActive;
    10.       }
    11. }
    12. ...
    13. ...
    14.  
    So the trick here is the use of renderer.material.mainTexture.

    Hope this help.
     
  4. Jesse Anders

    Jesse Anders

    Joined:
    Apr 5, 2008
    Posts:
    2,857
    Based on the code the OP posted, it looks like he or she is using C#, not UnityScript. (The example may still be relevant, but the specific syntax related to variable declaration won't be.)
     
  5. HrC123

    HrC123

    Joined:
    Feb 2, 2011
    Posts:
    140
    aha, Tank you very much
    but Jesse what should go on the place of Texture[] then?
    and I am "he" and I use C#

    ciaravoh this is what I want
    I want buttons, they are all white, when I click button it turns red, and it is red forever,
    then when I click again on RED button it turns to white (they would work more like checking boxes)

    I already made that
    :
    (this is in OnGUI thing)

    btnWT is button White Texture (it is public var and the texture is assigned)
    btnRT is button Red Texture (also assigned)
    btnT is current texture of each button (that's why it has to be arrayed)
    btnS is current button state integer ARRAYED variable which sends number (0-white 1-red) to the next script which reads that numbers and makes something based on that number
    Code (csharp):
    1.  
    2. // k = NUMBER OF ROWS (THIS CHECKS EVERY ROW)
    3. for (int k = 1; k <= 32; k++)
    4.     {
    5. // l = NUMBER OF COLUMNS (THIS CHECKS EVERY COLUMN (or every button in each row))
    6.     for (int l = 1; l <= 32; l++)
    7.         {
    8. //SETTING ALL TEXTURE INDEXES TO WHITE
    9.         btnT[((k-1)*32)+l] = btnWT;
    10.                                
    11. //MAKING BUTTONS
    12.         if (GUI.Button(new Rect(100 + btnsize * k, 100 + btnsize * l, btnsize, btnsize), btnT[((k-1)*32)+l]))
    13.             {
    14. //CLICK WHILE WHITE-CHECK              
    15.                 if (btnT[((k-1)*32)+l] == btnWT)
    16.                     {
    17.                         btnT[((k-1)*32)+l] = btnRT;
    18.                         Debug.Log("white to red");
    19.                         btnS[((k-1)*32)+l] = 1;
    20.                     }
    21. //CLICK WHILE RED-CHECK
    22.                 else
    23.                     {
    24.                         btnT[((k-1)*32)+l] = btnWT;
    25.                         Debug.Log("red to white");
    26.                         btnS[((k-1)*32)+l] = 0;
    27.                     }
    28.             }
    29.         }
    30.     }
    EXPLANATION:
    this script makes 32 * 32 button web/net AND checks if they are clicked AND if they are, it fires action

    WHAT I NEED:
    but I need to make ARRAYED VARIABLE for texture

    AND:
    and please if you can explain in few sentances What is the RAYCAST and for what should I use it?
    I mean I saw a lot of posts that are solved with using of RAY/RAYCAST or whatever it is called.
    I want to know what is it (I am reading the help file now but I don't understand everything)
    "bool - True when the ray intersects any collider, otherwise false."
    what is the RAY?
     
    Last edited: Feb 3, 2011
  6. geetoo

    geetoo

    Joined:
    Apr 21, 2009
    Posts:
    42
    Raycast is used to create a virtual ray from a [Vector3] origin and in a [Vector3] direction for a [float] length. And it will return information about the colliders the ray hit. Most of the time you want to use it to detect collisions between objects or clicked elements.

    Looking at ciaravoh's code
    Code (csharp):
    1. var ray = touchCamera.ScreenPointToRay (touch.position);
    2. if (Physics.Raycast (ray, hit, 15000))
    3. {
    4.      if(hit.transform.tag == "YOUR-OBJECT-TAG"  touchCount == 1)
    5.       {
    6.     hit.transform.renderer.material.mainTexture = TextureActive;
    7.       }
    8. }
    The first line converts the 2D touch position on the screen into a 3D point directed in the forward orientation of the touchCamera. Then the if statement checks if there is something in front of the camera in the next 15000 meters. This statement will be true if it's the case, false otherwise. In case of a collision detected, the script checks the object tag (+ the number of fingers on the screen for a single touch only), and it will change the object's texture to TextureActive.

    Hope this helps.
     
  7. HrC123

    HrC123

    Joined:
    Feb 2, 2011
    Posts:
    140
    ok it is nicely explained :) thank you


    and now I checked, something doesn't work in my code :/

    Texture[] btnT = new Texture[1024];
    this is the arrayed texture variable but

    "Assets/Scripts/MakeGame.cs(49,17): error CS0103: The name `btnT' does not exist in the current context"

    49th line is

    "btnT[((k-1)*32)+l] = btnWT;"

    both variables are textures...
    this index code exists 100%...
     
  8. geetoo

    geetoo

    Joined:
    Apr 21, 2009
    Posts:
    42
    From what I understand your script should look like this
    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class RedWhiteButtons : MonoBehaviour {
    5.  
    6.     public Texture2D btnWT;
    7.     public Texture2D btnRT;
    8.     public int btnsize = 32;
    9.  
    10.     private Texture2D[] btnT;
    11.     private int[] btnS;
    12.  
    13.     void Start()
    14.     {
    15.         btnT = new Texture2D[1024];
    16.         btnS = new int[1024];
    17.     }
    18.  
    19.     void OnGUI()
    20.     {
    21.         // k = NUMBER OF ROWS (THIS CHECKS EVERY ROW)
    22.         for (int k = 1; k <= 32; k++)
    23.         {
    24.             // l = NUMBER OF COLUMNS (THIS CHECKS EVERY COLUMN (or every button in each row))
    25.             for (int l = 1; l <= 32; l++)
    26.             {
    27.                 //SETTING ALL TEXTURE INDEXES TO WHITE
    28.                 [COLOR="red"]btnT[((k - 1) * 32) + l] = btnWT;[/COLOR]
    29.  
    30.                 //MAKING BUTTONS
    31.                 if (GUI.Button(new Rect(100 + btnsize * k, 100 + btnsize * l, btnsize, btnsize), btnT[((k - 1) * 32) + l]))
    32.                 {
    33.                     //CLICK WHILE WHITE-CHECK              
    34.                     if (btnT[((k - 1) * 32) + l] == btnWT)
    35.                     {
    36.                         btnT[((k - 1) * 32) + l] = btnRT;
    37.                         Debug.Log("white to red");
    38.                         btnS[((k - 1) * 32) + l] = 1;
    39.                     }
    40.                     //CLICK WHILE RED-CHECK
    41.                     else
    42.                     {
    43.                         btnT[((k - 1) * 32) + l] = btnWT;
    44.                         Debug.Log("red to white");
    45.                         btnS[((k - 1) * 32) + l] = 0;
    46.                     }
    47.                 }
    48.             }
    49.         }
    50.     }
    51. }
    52.  
    I get no error compiling this script... but I do get an "IndexOutOfRangeException: Array index is out of range." on the red line.
    Just an idea, but shouldn't you replace
    Code (csharp):
    1. ((k - 1) * 32) + l
    by
    Code (csharp):
    1. ((k - 1) * 32) + (l-1)
    since your l variable goes from 1 to 32 and not 0 to 31 ?
     
  9. HrC123

    HrC123

    Joined:
    Feb 2, 2011
    Posts:
    140
    huh
    there is some problem with indexing in texture arrays...

    ok just to give you a image of the thing, it looks like this without arrays:
    $Bez naslova.png

    ok next:
    the problems are:
    Assets/Scripts/MakeGame.cs(53,97): error CS0103: The name `btnT' does not exist in the current context
    Assets/Scripts/MakeGame.cs(53,25): error CS1502: The best overloaded method match for `UnityEngine.GUI.Button(UnityEngine.Rect, string)' has some invalid arguments
    Assets/Scripts/MakeGame.cs(53,25): error CS1503: Argument `#2' cannot convert `object' expression to type `string'
    Assets/Scripts/MakeGame.cs(56,37): error CS0103: The name `btnT' does not exist in the current context
    Assets/Scripts/MakeGame.cs(58,49): error CS0103: The name `btnT' does not exist in the current context
    Assets/Scripts/MakeGame.cs(63,49): error CS0103: The name `btnT' does not exist in the current context

    and the code:

    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class MakeGame: MonoBehaviour
    5. {
    6.     private Rect windowRect;
    7.     public GameObject PlanePrefab;
    8.     private float buttonWidth = 100f;
    9.     private float buttonHeight = 50f;  
    10.     public Texture2D btnWT;
    11.     public Texture2D btnRT;
    12.     private float btnsize;
    13.    
    14. void Start ()
    15.     {
    16.    
    17.     Texture2D[] btnT = new Texture2D[1025];
    18.     windowRect = new Rect( 0, 0, Screen.width, Screen.height);
    19.     btnsize = 20f;
    20.     for (int z= 0; z<= 1025; z++)
    21.         {
    22.             btnT[z] = btnWT;
    23.         }
    24.     }
    25.    
    26.    
    27. void Update ()
    28.     {
    29.    
    30.     }
    31.    
    32. void OnGUI()    
    33.     {
    34.    
    35.        
    36.         if (GUI.Button(new Rect(Screen.width / 2f + 250f, buttonHeight / 2f, buttonWidth, buttonHeight), "Mine Menu"))
    37.             {
    38.                 Application.LoadLevel(0);
    39.             }
    40.         if (GUI.Button(new Rect(Screen.width / 2f + 250f, buttonHeight / 2f + (10f + buttonHeight)*1f, buttonWidth, buttonHeight), "Save Map"))
    41.             {
    42.                 Debug.Log("Does nothing");
    43.             }
    44.         if (GUI.Button(new Rect(Screen.width / 2f + 250f, buttonHeight / 2f + (10f + buttonHeight)*2f, buttonWidth, buttonHeight), "Load Map"))
    45.             {
    46.                 Debug.Log("Does nothing");
    47.             }
    48.        
    49.     for (int K = 1; K <= 32; K++)
    50.     {
    51.     for (int L = 1; L <= 32; L++)
    52.         {
    53.         [COLOR="red"]if (GUI.Button(new Rect(16f + btnsize * K,14f + btnsize * L, btnsize, btnsize), btnT[ (K - 1) * 32 + (L-1)] )) [/COLOR]
    54.             {
    55.                
    56.                 [COLOR="red"]if (btnT[ (K - 1) * 32 + (L-1)] == btnWT) [/COLOR]
    57.                     {
    58.                         [COLOR="red"]btnT[ (K - 1) * 32 + (L-1)] = btnRT;[/COLOR]
    59.                         Debug.Log("white to red");
    60.                     }
    61.                 else
    62.                     {
    63.                         [COLOR="red"]btnT[ (K - 1) * 32 + (L-1)] = btnWT;[/COLOR]
    64.                         Debug.Log("red to white");
    65.                     }
    66.             }
    67.         }
    68.     }
    69.     }
    70. }
     
  10. geetoo

    geetoo

    Joined:
    Apr 21, 2009
    Posts:
    42
    You are creating your btnT array inside the Start function. Therefore it is only known inside it. What you need to do to solve this is define it as a class member
    Code (csharp):
    1. private Texture2D[] btnT;
    and then only initialize it in the Start function
    Code (csharp):
    1. btnT = new Texture2D[1025];
    .

    Hope this helps.
     
  11. HrC123

    HrC123

    Joined:
    Feb 2, 2011
    Posts:
    140
    yes, thank you