Skip to main content
Adjusted indentation of code so it is easier to read.
Source Link

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));
     }
 }

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));
     }
 }

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));
    }
}
Source Link
cmprogram
  • 267
  • 1
  • 12

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));
     }
 }