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.
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
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.
Unity version
2019.3.4f1
-
Answer # 1
Related articles
- c# - i got an error when trying to fade out in unity
- c # - code error of script implemented by unity cannot be resolved
- c# - [unity] i want to change the rich text of textmeshpro from a script
- unity - i get an error that the guid suffers when importing a package
- unity c # error code indexoutofrangeexception: index was outside the bounds of the array
- c# - i want to acquire the collider tags of the part hit by unity separately
- c# - i want to change the texturetype of texture2d from a script
- c# - [unity] hit detection of tile map after adding composite collider 2d
- c# - coroutines executed before unity scene move are not executed
- c# - method detection by instantiated gameobjects
- unity - about cs0101 error
- c # - [unity] how to make a script that executes processing after a certain period of time
- c# - [unity] i want to call a variable whose value has been changed from another script
- unity: script cannot be added
- c # - i can't attach a script in unity (beginner)
- i can't get rid of the error i get when installing unity from unity hub
- unity - gui error: an error occurs when creating a folder or script
- unity - script invalidation does not work
- c# - [unity] i do not know how to access the variables of other methods from a specific method
- c# - i want to find out if there is a "target" in the {unity} list
- c# : Unity | Can't add getting coins for watching rewarded ads
- c# : Inheriting a component from a superclass
- c# : KeyNotFoundException: The given key was not present in the dictionary
- c# : The Strategy pattern in Unity. How to fit the logic of click-to-move motion into this pattern?
- c# : Overlay background on top of each other
- c# : Incorrect Unity3D motion calculation
- c# : InvalidCastException: Specified cast is not valid
- c# : How do I get a component in Unity through a string variable?
- c# : Is it correct to write logic in scriptableObject?
- c# : How to walk both up and down?
According to https://doc.photonengine.com/ja-jp/pun/v2/gameplay/instantiation
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.