Home>

I have an error in the code below that I can't figure out why and I want to fix it.

NullReferenceException: Object reference not set to an instance of an object
UnkomanColTri.OnTriggerEnter (UnityEngine.Collider other) (at Assets/C#Script/PlayerScript/UnkomanColTri.cs:41)


This error occurs in the 4th line from the bottom of the code below. (The code below shows only the part relevant to this question)

This line should only be called when you hit the explosion, but the error comes out the moment you run Scene.
Also, this error does not occur every time, but it occurs about once every 4 to 5 times.
And when an error occurs, this error will always occur twice. There is only one of this code in the scene.

If Rb is not instantiated, it should not be possible to use Rb at the time of the explosion when an error occurs.
However, even if an error occurs, if it hits an explosion, the process using Rb will be executed without any problems.
In addition, it seems strange to me that this error occurs only in the fourth line from the bottom. I'm doing the same process using Rb in the upper line, but I don't understand why the error occurs in the 4th line from the bottom.

Summary,
-This error only occasionally occurs.
・When you go out, you get two (only one player)
・When it comes out, it comes out the moment the Scene starts
・No error will occur even if this code is called in case of an explosion
・Code that uses Rb above the code that gives the error
Is not an error.
is.

Applicable source code
  void Start()
    {
        Rb = GetComponent<Rigidbody>();
    }
    void OnCollisionEnter(Collision other)
    {
        //Set collision to true when hitting the sea
        if(other.gameObject.tag == "Sea") contact = true;
        // Blow off if you hit Unko Bullet
        if (other.gameObject.tag == "Bullet")
        {
            Rb.AddForce(Random.onUnitSphere * 70, ForceMode.Impulse);
            blownMode = true;
        }
    }
    void OnTriggerEnter(Collider other)
    {
        if (other.gameObject.tag == "Explosion")
        {
            Rb.AddForce(Random.onUnitSphere * 90, ForceMode.Impulse);
            blownMode = true;
        }
    }
What I tried

Since the player Obj to which this script is attached is a network object, we are creating it as a network object at the moment Scene starts.
So I thought that it could not be instantiated because GetComponent() was called before the player Obj was created. So I set the order in which the scripts are executed so that the script that instantiates the player Obj is executed before this code.
However, I still get an error.

If this hypothesis holds true in the first place, when an error occurs, you should not be able to use Rb for the times that could not be instantiated with GetComponent.
But even when I get an error, the code works and processes without problems.
No other code instantiates Rb.
So this temporary is wrong.

But I don't know what caused this error anymore.
"Maybe this is the cause?"
It's okay to speculate, so please tell me what seems to be the cause of this error.

Supplementary information (FW/tool ​​version, etc.)

Unity version
2019.3.4f1

  • Answer # 1

    According to https://doc.photonengine.com/ja-jp/pun/v2/gameplay/instantiation

    By default, PUN instantiate uses DefaultPool. This loads the prefab from the Resources folder and later destroys the GameObject. A more sophisticated IPunPrefabPool implementation returns the object to the Destroy's pool and reuses it in Instantiate. In that case, Instantiate does not actually create the GameObject. In other words, Start() is not called by Unity. Therefore, the Network Game Object script implements only OnEnable and OnDisable.

    In other words, it seems that Start() is not called when it is taken from the pool, but it is better to check it rather than guess it.

    Immediately before the line where the error appears, check if Rb is null before checking if it is null, and if it is null, output as much information as you can think of, such as the object name, Photon ID, collision partner information, etc. and output it. It is a repetition of thinking first and outputting information.