Search Unity

SetActive and more stuff

Discussion in 'Getting Started' started by Stradmann, Jan 15, 2019.

  1. Stradmann

    Stradmann

    Joined:
    Jun 11, 2018
    Posts:
    38
    Hi, Sorry for my english. I have some problems with my script. In the first place, seems that SetActive(true) doesn't work. I read other posts about it and i still don't know what happen. The script who do that is attached to GameController game object who is ever active couse their functions works. I drag and drop the reference to the object i want to active from Hierachy couse i don't have a prefab of this. This about the common errrors a read about. Then seems that program ignores a big part of for loop, including Set active(true) and WaitForSeconds, who is in IEnumerator function. And finnaly, if i activate handly the panels to see what happen, seems that only do the last round of the for loop. I can' find what mistakes i did..any help plese?

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5.  
    6. public class GameController : MonoBehaviour {
    7.  
    8.     //Conversation variables
    9.     public GameObject dialogPanel1;
    10.     public GameObject dialogPanel2;
    11.     public Image portrait1;
    12.     public Image portrait2;
    13.     public Text dialog1;
    14.     public Text dialog2;
    15.    
    16.     public GameRecord gameRecord;
    17.  
    18.     public Conversation initialConversation;
    19.  
    20.     private void Start()
    21.     {
    22.         PrepareInterface();
    23.         if (!gameRecord.estaComençadaLaPartida)
    24.         {
    25.             ConversationAction(initialConversation);
    26.         }
    27.         SetUpGame();
    28.     }
    29.  
    30.     private void PrepareInterface()
    31.     {
    32.         dialogPanel1.SetActive(false);
    33.         dialogPanel2.SetActive(false);
    34.     }
    35.  
    36.     private void ConversationAction (Conversation conversa)
    37.     {
    38.         char[] delimiterCharacters = { '\n' };
    39.         string[] separatedDialog1 = conversa.char1DialogLines.Split(delimiterCharacters);
    40.         string[] separatedDialog2 = conversa.char2DialogLines.Split(delimiterCharacters);
    41.         for (int i = 0; i< separatedDialog1.Length; i++)
    42.         {
    43.             dialogPanel1.SetActive(true);
    44.             portrait1.sprite = conversa.char1Portrait;
    45.             dialog1.text = separatedDialog1[i];
    46.             StartCoroutine(WaitToRead(2.0f));
    47.  
    48.             dialogPanel2.SetActive(true);
    49.             portrait2.sprite = conversa.char2Portrait;
    50.             dialog2.text = separatedDialog2[i];
    51.             StartCoroutine(WaitToRead(2.0f));
    52.  
    53.             dialogPanel1.SetActive(false);
    54.             dialogPanel2.SetActive(false);
    55.             StartCoroutine(WaitToRead(0.5f));
    56.         }
    57.         Debug.Log("Manteniendo conversacion");
    58.     }
    59.  
    60.     private void SetUpGame()
    61.     {
    62.         Debug.Log("Iniciando partida");
    63.     }
    64.  
    65.     private IEnumerator WaitToRead (float time)
    66.     {
    67.         yield return new WaitForSeconds(time);
    68.     }
    69. }
     
  2. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    SetActive works fine. The issue is that you don't understand how coroutines work. On line 46 you start the WaitToRead coroutine, and then the code immediately goes on to line 47 (well, 48). WaitToRead will run later. In fact it will simultaneously run 3*separatedDialog1.Length times, all at once, because that's how many times you start it.

    Coroutines are confusing and I generally recommend beginners avoid them entirely.

    But if you feel you must use them, then you need to make ConversationAction an IEnumerator, and call StartCoroutine only on that. Delete the WaitToRead method, and have ConversationAction do the
    yield return new WaitForSeconds
    thing.
     
  3. Stradmann

    Stradmann

    Joined:
    Jun 11, 2018
    Posts:
    38
    I see! Thanks a lot!
     
    JoeStrout likes this.