0
\$\begingroup\$

I made this code because I needed an image to display a couple seconds after an enemy spawns, but for some reason I can't make it work.

There's no error in the console, but it doesn't display the image and I don't know what to do. Here's my script (it's attached to the enemy prefab)

 public class enemyAttack : MonoBehaviour {

 public float time = 5f;
 private IEnumerator coroutine;    
 public Image image; 
 private float t = 5f;

 void Start ()
 {
     //image.gameObject.SetActive (false);
     image.enabled = false;
     coroutine = nombredeCorutina (time, image); 
     StartCoroutine (coroutine);
 }

 private IEnumerator nombredeCorutina (float t, Image im)
 {

     image.enabled = true;
     //im.gameObject.SetActive (true);
     yield return new WaitForSeconds (t);

     coroutine = nombredeCorutina (time, im);
     StartCoroutine (coroutine);
 }
}
\$\endgroup\$
  • \$\begingroup\$ enemy destroy script? please \$\endgroup\$ – Muhammad Faizan Khan Nov 13 '17 at 4:31
  • \$\begingroup\$ The code is actually working. i have tested and image is displaying. \$\endgroup\$ – Muhammad Faizan Khan Nov 13 '17 at 4:32
  • 1
    \$\begingroup\$ Not concerning the question itself, but the coding style: It is strongly discouraged to mix english names with other languages in the same project, as it highly decreases readability. Also you might want to consider removing the parts of the code that are commented out, to create a minimal code example. \$\endgroup\$ – Raimund Krämer Nov 13 '17 at 9:58
  • \$\begingroup\$ I'd like to add that this is over-complicating what should be a simple Invoke() call. You really shouldn't get stuck on one method but try all methods available instead. \$\endgroup\$ – John Hamilton Nov 13 '17 at 10:15
  • \$\begingroup\$ Is this object nested within a Canvas object? Image will only be rendered when nested in a Canvas. That could be your issue. \$\endgroup\$ – Tartle Wizard Nov 13 '17 at 22:09
2
\$\begingroup\$

You need to edit your code something like this:

public float time = 5f;
private IEnumerator coroutine;
public Image image;
private float t = 5f;

void Start() {
    image.enabled = false;
}

private IEnumerator nombredeCorutina(float t, Image im) {
    yield return new WaitForSeconds(t);
    image.enabled = true;
}

public void EnemySpawn() {
    //call enemey spawn whenver you want. it will register coroutine and you image will be displayed after given seconds
    //enemyspawn logic
    StartCoroutine(nombredeCorutina(t,image));
}

Now you have to identify that when should you disable your image and when you want to spawn enemy.

\$\endgroup\$
  • 1
    \$\begingroup\$ This is the best answer here. The reason for that is that it correctly moves the image.enabled = true; call after the yield return new WaitForSeconds(t); call as well as fixing the self-calling coroutine that wasn't needed and creating a dedicated EnemySpawn() method from which the coroutine is activated in. \$\endgroup\$ – Draco18s Jan 16 '18 at 7:23
0
\$\begingroup\$

Some of your code is redundant or actively incorrect. For instance, why set the cooroutine in private and redeclare it over and over when you can just call it as is?

I've reordered your code to work correctly for the specific purposes you need it, if you wish to make your code reusable (which you should) then you'd have to structure it differently.

public class enemyAttack : MonoBehaviour {

    public float time = 5;   
    public GameObject gameObjectHoldingImage; //Obviously too long a name, but helps explain. 

    void Start (){
        gameObjectHoldingImage.SetActive (false);
        StartCoroutine(nombredeCorutina (time));
    }

    private IEnumerator nombredeCorutina (float t){         
        gameObjectHoldingImage.SetActive(true);
        yield return new WaitForSeconds (t);
        //If you call this again (repeating) it is redundant as the game 
        //object/image is not re-set to false. 
        StartCoroutine (nombredeCorutina (t));
    }
}
\$\endgroup\$
  • 1
    \$\begingroup\$ This answer unfortunately doesn't actually solve the problem as it sets the image to being active/visible before waiting for 5 seconds and doesn't remove the unneeded self-calling coroutine portion (although it does comment on it). \$\endgroup\$ – Draco18s Jan 16 '18 at 7:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.