StartCoroutine Null Error

Hello All I want to make my player blink when her is his and here is my code. But i keep getting a NullREference Exception at StartCoroutine(Wait(3.0f))… any ideas

    IEnumerator Wait(float waitTime)
    {
        yield return new WaitForSeconds (waitTime);
    }


    private void Pdamaged (){
        Phit = GameObject.Find ("Graphics");
        Phit.GetComponent<Renderer>().enabled = false;
        StartCoroutine(Wait(3.0f));
        Phit.GetComponent<Renderer>().enabled = true;
        StartCoroutine(Wait(3.0f));
        Phit.GetComponent<Renderer>().enabled = false;
        StartCoroutine(Wait(3.0f));
        Phit.GetComponent<Renderer>().enabled = true;
        StartCoroutine(Wait(3.0f));
    }

still get a NullReferenceException

First of all, I don’t think this does what you think it does.
It’s just going to start 4 separate coroutines at the same time that wait for 3 seconds in parallel. It’s not going to temporally space out those lines between them.

Secondly, can you post the whole error log and not just “NullReferenceException”? It doesn’t make sense that StartCoroutine(Wait(3.0f)); would throw that exception. There aren’t any references on that line to be null.

hmmm i see what you are saying Pharan. I just want it where the player gets it then flickers on and off for a sec or two telling the user he was hit…
here is the full error

NullReferenceException
Player.Pdamaged () (at Assets/Player.cs:22)
Player.AddPX () (at Assets/Player.cs:50)
PoopDamage.OnTriggerEnter2D (UnityEngine.Collider2D other) (at Assets/PoopDamage.cs:14)

Try something like this:

    IEnumerator Flicker(Renderer theRenderer, float waitTime, int flickerCount)
    {
        for (int i = 0; i < flickerCount; i ++)
        {
            theRenderer.enabled = false;
            yield return new WaitForSeconds (waitTime);
            theRenderer.enabled = true;
            yield return new WaitForSeconds (waitTime);
        }
    }
   
    void Pdamaged()
    {
        Renderer theRenderer = GameObject.Find ("Graphics").GetComponent<Renderer>();
        StartCoroutine(Flicker(theRenderer, 3.0f, 2));
    }

Then you’d start a single coroutine that did that:

private void Pdamaged()
{
    Phit = GameObject.Find("Graphics");
    var r = Phit.GetComponent<Renderer>();
    this.StartCoroutine(this.Flicker(r, 2, 3f));
}

private IEnumerator Flicker(Renderer r, int flickers, float pause)
{
    var wait = new WaitForSecond(pause):
   
    for(int i = 0; i < flickers; i++)
    {
        r.enabled = false;
        yield return wait;
        r.enabled = true;
        yield return wait;
    }
}

[edit]
lol, artaka posted pretty much identical code to me

1 Like

I still get this error
NullReferenceException
UnityEngine.MonoBehaviour.StartCoroutine (IEnumerator routine) (at C:/buildslave/unity/build/artifacts/generated/common/runtime/UnityEngineMonoBehaviourBindings.gen.cs:62)
Player.Pdamaged () (at Assets/Player.cs:27)
Player.AddPX () (at Assets/Player.cs:58)
PoopDamage.OnTriggerEnter2D (UnityEngine.Collider2D other) (at Assets/PoopDamage.cs:14)

sounds to me like a renderer doesn’t exist on the GameObject ‘Graphics’ that is found by GameObject.Find.

Personally I avoid the use of GameObject.Find.

Try making it a property of your script that has to be directly set.

Does your Player class extend Monobehaviour? You can only start coroutines on classes that extend it.

How would I check or implement a class extend monobehaviour?

I have tried setting the game object as a public and assigning it to in the inspector instead of using GameObject.Find but that still trows me the error.
Still is throwing the same null error
I am using Artakas code above which is as fallows

    IEnumerator Flicker(Renderer theRenderer, float waitTime, int flickerCount)
    {
        for (int i = 0; i < flickerCount; i ++)
        {
            theRenderer.enabled = false;
            yield return new WaitForSeconds (waitTime);
            theRenderer.enabled = true;
            yield return new WaitForSeconds (waitTime);
        }
    }

    private void Pdamaged (){


        Renderer theRenderer = GameObject.Find ("Graphics").GetComponent<Renderer>();
        StartCoroutine(Flicker(theRenderer, 3.0f, 2));

    }

There needs to be both a GameObject called “Graphics” in your scene and also a Renderer component on that gameobject. You are missing either 1 or both of those things. Post your full script, post a screencapture of your scene setup.

I think I have both. Here is a screen shot and full line of code. The graphics are only place holders and are not mine. The code is not that clean because I am not a big coder as you can tell.

http://i57.tinypic.com/2vkg6lx.jpg

using UnityEngine;
using System.Collections;

public class Player : MonoBehaviour {

    public class PlayerStats {
        public int Health = 100;
    }

    public GameObject Phit;

    IEnumerator Flicker(Renderer theRenderer, float waitTime, int flickerCount)
    {
        for (int i = 0; i < flickerCount; i ++)
        {
            theRenderer.enabled = false;
            yield return new WaitForSeconds (waitTime);
            theRenderer.enabled = true;
            yield return new WaitForSeconds (waitTime);
        }
    }

    private void Pdamaged (){


        Renderer theRenderer = GameObject.Find ("Graphics").GetComponent<Renderer>();
        StartCoroutine(Flicker(theRenderer, 3.0f, 2));

    }

    public PlayerStats playerStats = new PlayerStats();


    private int xStat = 0;
    private GameObject X1;
    private GameObject X2;
    private GameObject X3;
    private GameObject X4;


    public void AddPX (){
        X1 = GameObject.Find("X1");
        X2 = GameObject.Find("X2");
        X3 = GameObject.Find("X3");
        X4 = GameObject.Find("X4");
        Phit = GameObject.Find ("Graphics");

        if (xStat == 0 ){
            X1.GetComponent<Renderer>().enabled = true;
            Pdamaged ();
            xStat = 1;

        }
 
        else if(xStat == 1 ){
            Debug.Log ("2");
            X2.GetComponent<Renderer>().enabled = true;
            xStat++;
        }

        else if(xStat == 2 ){
            X3.GetComponent<Renderer>().enabled = true;
            xStat++;
        }

        else if(xStat == 3 ){
            X4.GetComponent<Renderer>().enabled = true;
            xStat++;
        }

 
    }



}

And you also have GameObjects named: X1, X2, X3, X4 all with Renderers attached?

Yes all of the X ones work. Those are to put the X on-top of the soccer balls in the top right.

Since the "X"s are working we won’t touch those. For “Phit”, make sure that it’s assigned the “Graphics” gameObject by dragging “Graphics” onto the Phit slot in the inspector. Doing so will ensure that you have that variable assigned and you don’t have to use GameObject.Find to assign it. After doing so, remove line 46 and change line 26 to:

 Renderer theRenderer = Phit.GetComponent<Renderer>();

Hope that helps.

here you can see i changed the code to use the Phit and then also a picture showing it in the inspector.

    public GameObject Phit;

    IEnumerator Flicker(Renderer theRenderer, float waitTime, int flickerCount)
    {
        for (int i = 0; i < flickerCount; i ++)
        {
            theRenderer.enabled = false;
            yield return new WaitForSeconds (waitTime);
            theRenderer.enabled = true;
            yield return new WaitForSeconds (waitTime);
        }
    }

    private void Pdamaged (){


        Renderer theRenderer = Phit.GetComponent<Renderer>();
        StartCoroutine(Flicker(theRenderer, 3.0f, 2));

    }

and here is the full error i get still

NullReferenceException
UnityEngine.MonoBehaviour.StartCoroutine (IEnumerator routine) (at C:/buildslave/unity/build/artifacts/generated/common/runtime/UnityEngineMonoBehaviourBindings.gen.cs:62)
Player.Pdamaged () (at Assets/Player.cs:27)
Player.AddPX () (at Assets/Player.cs:50)
PoopDamage.OnTriggerEnter2D (UnityEngine.Collider2D other) (at Assets/PoopDamage.cs:14)

I have also change Phit instead of saying graphics i changed it to the soccer ball to see if that worked and still same issue when the player went into the collision box

Let’s try one more thing. Let’s eliminate Phil variable and manually assign the Renderer in the Inspector and see if you’re still getting a null error. Basically add a new public field “theRenderer” and drag the “Graphics” gameObject onto it in the Inspector and remove “Renderer theRenderer = Phit.GetComponent();” from Pdamaged. Here is what it should look like:

public Renderer theRenderer;

private void Pdamaged ()
{
    StartCoroutine(Flicker(theRenderer, 3.0f, 2));
}

Don’t forget to assign “theRenderer” in the Inspector.

Same issue… If you look at my code i had something call AddPX () which then called Pdamaged()… I even took out where it went through AddPX(). Now my object just calls Pdamaged() and that is it. Same stupid issue…i have no idea how it could possibly be null.