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

Question Assigning Materials freeze android game

Discussion in 'Scripting' started by slaga, Jun 11, 2021.

  1. slaga

    slaga

    Joined:
    Dec 3, 2018
    Posts:
    142
    I'm having an issue when it comes to changing materials on instantiated objects.
    Im using this function to swap some instantiated pieced and give them an outline through a shader.

    Code (CSharp):
    1.  public void StartGame()
    2.     {
    3.         preloadImage.SetActive(false);
    4.  
    5.         InterchangeRandom(shuffledArray);
    6.  
    7.         for (int i = 0; i < selectedArray.GetLength(0); i++)
    8.         {
    9.             float scaleWidth = 100 * ((float)shuffledArray[i][2]) / ((float)selectedArray[i][2]);
    10.             float scaleHeight = 100 * ((float)shuffledArray[i][3]) / ((float)selectedArray[i][3]);
    11.             spawnedGameObjects[i].transform.localScale = new Vector3(scaleWidth, scaleHeight, 0);
    12.  
    13.             float halfWidth = ((float)shuffledArray[i][2]) / ((float)2);
    14.             float halfHeight = ((float)shuffledArray[i][3]) / ((float)2);
    15.             spawnedGameObjects[i].transform.position = new Vector3(shuffledArray[i][0] - 660 + halfWidth, shuffledArray[i][1] - 450 + halfHeight, 0);
    16.             spawnedGameObjects[i].GetComponent<ClickOn>().MoveTo(spawnedGameObjects[i].transform.position);
    17.  
    18.             spawnedGameObjects[i].GetComponent<Renderer>().material = normal; // this part here
    19.         }
    20.      
    21.         isPlaying = true;
    22.     }
    My issue is that no matter how i try to use that material setup the game freezes for a couple of seconds and only when all objects have their material changed does it continue, and i have a timer at scene start so it stops at 1 for a couple of seconds then the game begins.

    I tried placing the material in a coroutine, i also though of making a singleton that loads them into an array at game start and then call that one to assign them but its still the same issue.
    this is the object i had in the main menu scene

    Code (CSharp):
    1. public class MatPreloader : Singleton<MatPreloader>
    2. {
    3.     public Material[] materials;
    4.  
    5.     private new void Awake()
    6.     {
    7.         materials = Resources.LoadAll("Pieces", typeof(Material)).Cast<Material>().ToArray();
    8.     }
    9. }
    and then in the game manager script that calls the initial function i used to do this:

    Code (CSharp):
    1. GetComponent<Renderer>().material = MatPreloader.instance.materials[0];
    Just to get a better understanding its a puzzle game and at start i show the image then cut it to pieces then shuffle it and give it an outline. After that once the user clicks on the pieces they do change materials again from a white outline to a red one. Once the initial loading of the materials happen then i have no issues with the rest of the gameplay, the same is true for each time i open the game after the first install and run. Can i somehow preload them? load the asynchronously so the timer wont stop and the game wont freeze?

    So far i have determined the freezing is because of the material change as when i disable it everything runs smoothly, note this is an android game, im having no issues in the editor!

    I've been trying for a couple of days and I'm getting desperate. thank you!
     
  2. slaga

    slaga

    Joined:
    Dec 3, 2018
    Posts:
    142
    edit: forgot to say this only happens with a shader outline material! if i just change the defaults material color nothing bad happens.
     
  3. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,756
    Don't write hairy code like this. Who knows where it is crashing?

    How to break down hairy lines of code:

    http://plbm.com/?p=248

    Break it up, practice social distancing in your code, one thing per line please.

    ALSO: never use the bare version of Resources.LoadAll(). Instead, use this version:

    Code (csharp):
    1. materials = Resources.LoadAll<Material>("MyMaterialsDirectory/");
     
  4. slaga

    slaga

    Joined:
    Dec 3, 2018
    Posts:
    142
    its crashing on this line right here!
    Code (CSharp):
    1. rend = GetComponent<Renderer>();
    2.         rend.material = normal;
    actually it freezes the game untill all materials are chaged. either having it in the start function of the instantiated game objects or having it inside the function that cuts the texture, once the materials are getting assigned the game freezes and stops untill all pieces have it. if i remove that line i have no isses. also the resources load all was an attempt which i do not use now. all materials are assigned in the inspector in public fields . If i change the default material color all is well, the issue lies with loading the outline shader material .