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. Dismiss Notice

Scripting for textures

Discussion in 'Scripting' started by Anang, Jun 16, 2014.

  1. Anang

    Anang

    Joined:
    Jun 16, 2014
    Posts:
    12
    Hi,

    I have been trying to load multiple textures through resource.loadall and link them with a settings menu. Part of code is working i.e. textures are getting loaded.

    However I am not able to link the code with the settings menu. What I am trying to do is to change the texture when I select the option in the settings menu. This is problem has taken a week of time and after hours of digging, I got no solution. Please help! Would really appreciate any inputs.

    Following is my code;
    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. using System.Collections;
    4.  
    5.  
    6.  
    7. public class text : MonoBehaviour
    8.  
    9. {
    10.  
    11. void Update()
    12.  
    13.     {
    14.  
    15. Object[] aImages = Resources.LoadAll("Textures");
    16.  
    17. Debug.Log("The size of the array is: " + aImages.Length);
    18.  
    19.     int i = 0;
    20.  
    21.     while (i < aImages.Length){
    22.  
    23.         Debug.Log("The item at position" + i + " is: " + aImages);
    24.  
    25.         i++;
    26.  
    27.     }
    28.  
    29.  
    30.  
    31.     Texture2D texImage0 = (Texture2D) aImages[0];
    32.  
    33.     renderer.material.mainTexture = texImage0;
    34.  
    35.  
    36.  
    37.     Texture2D texImage1 = (Texture2D) aImages[1];
    38.  
    39.     renderer.material.mainTexture = texImage1;
    40.  
    41.  
    42.  
    43.     Texture2D texImage2 = (Texture2D) aImages[2];
    44.  
    45.     renderer.material.mainTexture = texImage2;
    46.  
    47.  
    48.  
    49. }
    50.  
    51.     public void ChangeCubesBG(string mCubesBG)
    52.  
    53. {
    54.  
    55.     if (mCubesBG == "bk1")
    56.  
    57.        t = new Texture2D(texImage1);
    58.  
    59.  
    60.  
    61.     if (mCubesBG == "bk2")
    62.  
    63.        t = new Texture2D(texImage2);
    64.  
    65.      
    66.  
    67.     GameObject.FindGameObjectWithTag("BkTag").material.texture = t;
    68.  
    69. }
    70.  
    71. }
     
    Last edited: Jun 16, 2014
  2. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,738
  3. Anang

    Anang

    Joined:
    Jun 16, 2014
    Posts:
    12
    Hi.. Have edited the same.. would appreciate your help here...
     
  4. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    A quick search of the API documentation would have revealed that there is no 'material' member of GameObject nor is there a 'texture' member of Material. This should have provided you a compile-time error message that would have been helpful to post along with your code. What you probably want is something like this

    Code (csharp):
    1.  
    2. GameObject.FindGameObjectWithTag("BkTag").renderer.material.mainTexture= t;
    3.  
    You're also loading all of your textures every frame and then casting them individually. LoadAll has a generic overload that will cast the entire array for you

    Code (csharp):
    1.  
    2. Texture2D[] alImages = Resources.LoadAll<Texture2D>("path");
    3.  
     
  5. Anang

    Anang

    Joined:
    Jun 16, 2014
    Posts:
    12
    Hi... thanks for pointing that. However upon correcting that, the error that keeps on coming is

    "error CS0103: The name `texImage1' does not exist in the current context" Any insight here?
     
  6. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    You don't have a variable named texImage1 or you do and it's in the wrong scope.

    Googling these error messages should provide good insight.
     
  7. Anang

    Anang

    Joined:
    Jun 16, 2014
    Posts:
    12
    Hey I have tried searching about the error. I am using texImage1 as variable name since the texture in the array is being rendered by this name only (texImage1, texImage2 and so on) I think I am using the wrong syntax when I am calling the variable. Can you correct me here? I am stuck with this issue for days now.. plz help!!!
     
  8. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    You'll have to post your current code.
     
  9. Anang

    Anang

    Joined:
    Jun 16, 2014
    Posts:
    12
    Current code is as below;

    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. using System.Collections;
    4.  
    5.  
    6.  
    7. public class bb : MonoBehaviour
    8.  
    9. {  
    10.  
    11. void Update()
    12.  
    13.     {  
    14.  
    15. Object[] aImages = Resources.LoadAll("Textures");  
    16.  
    17. Debug.Log("The size of the array is: " + aImages.Length);
    18.  
    19.     int i = 0;
    20.  
    21.     while (i < aImages.Length){
    22.  
    23.         Debug.Log("The item at position" + i + " is: " + aImages[i]);
    24.  
    25.         i++;
    26.  
    27.     }
    28.  
    29.    
    30.  
    31.     Texture2D texImage0 = (Texture2D) aImages[0];
    32.  
    33.     renderer.material.mainTexture = texImage0;
    34.  
    35.    
    36.  
    37.    
    38.  
    39. }
    40.  
    41.     public void ChangeCubesBG(string mCubesBG)
    42.  
    43. {
    44.  
    45.     if (mCubesBG == "bk1")
    46.  
    47.        t = new Texture2D(texImage1);
    48.  
    49.  
    50.     if (mCubesBG == "bk2")
    51.  
    52.        t = new Texture2D(texImage2);
    53.  
    54.        
    55.  
    56.     GameObject.FindGameObjectWithTag("BkTag").renderer.material.mainTexture = t;
    57.  
    58. }
    59.  
    60. }
    61.  
     
  10. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    You have no variable named texImage1. Also - doing stuff like texImage0, texImage1, etc completely defeats the point of having an array.

    Your'e also still not using the generic overload to get a Texture2D[] instead of an Object[] and you're still loading the textures every frame,
     
  11. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,738
    Your immediate problem: you are using texImage1 and texImage2 without ever declaring them anywhere. From your previous post, it sounds like you believe this is a way to access your aImages array, and if that is what you're thinking, that is wrong. "t" is also undeclared.

    As has been mentioned in previous comments, these things should all be creating errors when the script is compiled. (errors that tell you exactly what we're telling you) If you don't understand what the errors mean or how to fix them, that's one thing, but you DO know the errors are there, right?
     
  12. Anang

    Anang

    Joined:
    Jun 16, 2014
    Posts:
    12
    Ah there was only one error as below and I understand that was since I had not declared the variables.

    "The name `texImage1' does not exist in the current context"

    Also using Texture2D[] alImages = Resources.LoadAll<Texture2D>("path"); keeps generating below error;

    The non-generic method `UnityEngine.Resources.LoadAll(string)' cannot be used with the type arguments

    Following is the revised code.. still no errors nor it is yielding the desired result. Please help in sorting this..

    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. using System.Collections;
    4.  
    5. public class bb : MonoBehaviour
    6.  
    7. {
    8.  
    9. public string mCubesBG;
    10.  
    11. void Update()
    12.  
    13. {
    14.  
    15. Object[] aImages = Resources.LoadAll("Textures");
    16.  
    17.  
    18. Debug.Log("The size of the array is: " + aImages.Length);
    19.  
    20.  
    21.     int i = 0;
    22.  
    23.  
    24.     while (i < aImages.Length){
    25.  
    26.  
    27.         Debug.Log("The item at position" + i + " is: " + aImages);
    28.  
    29.  
    30.         i++;
    31.  
    32.  
    33.     }
    34.  
    35.  
    36.       if(mCubesBG =="bk1")
    37.  
    38.     {
    39.  
    40. Texture2D texImage0 = (Texture2D) aImages[0];
    41.  
    42. renderer.material.mainTexture = texImage0;
    43.  
    44.     }
    45.  
    46.     else if(mCubesBG =="bk2")
    47.  
    48.     {
    49.  
    50.     Texture2D texImage1 = (Texture2D) aImages[1];
    51.  
    52.     renderer.material.mainTexture = texImage1;
    53.  
    54.         }
    55.  
    56.  
    57.     }
    58.  
    59. }
    60.