I’m getting a NullReferenceException in this script
public class ContainerBehaviour : MonoBehaviour
{
private TouchControl touchControl;
public void DropZoneStartPos()
{
touchControl = new TouchControl();
if( gameObject.layer == LayerMask.NameToLayer( "Upper Container" ) )
{
if( gameObject.transform.position.x == touchControl.dropZoneArr[0].transform.position.x )
{
touchControl.dropZoneArr[0].transform.position = new Vector2( gameObject.transform.position.x, gameObject.transform.position.y + 2.5f );
Debug.Log(touchControl.dropZoneArr[0].transform.position);
}
}
}
void OnTriggerEnter( Collider other )
{
if( other.gameObject.layer == LayerMask.NameToLayer( "Upper Container" ) && other.transform.position.y > gameObject.transform.position.y &&
other.gameObject.tag != "Drop Zone" )
{
gameObject.layer = LayerMask.NameToLayer( "Lower Container" );
}
}
}
The NullReferenceException is on this line: if( gameObject.layer == LayerMask.NameToLayer( “Upper Container” ) )
This script is attached to many game objects that either have a “Lower Container” layer or a “Upper Container” layer attached to them. I manually attach the layers in the editor. When the game starts another script references this script and the error occurs.
What I find really strange is that there is no problem when the OnTriggerEnter() runs on this line: if( other.gameObject.layer == LayerMask.NameToLayer( “Upper Container” )
Does anybody have an idea of what is happening?
Thanks in advance.