Question by
Sonata14 · Oct 11, 2020 at 02:19 PM ·
c#rigidbodytriggerrigidbody2d
Object reference not set to an instance of an object
Hello! I am new in programming C# and I am trying to make a "flappy bird" game. I feel stupid that I can not find the solution here, I hope you may help me. The error message is :
"
NullReferenceException: Object reference not set to an instance of an object TapController.OnTriggerEnter2D (UnityEngine.Collider2D col) (at Assets/Scripts/TapController.cs:52)
"
Here is my TapController code: I would glad for any help. Thanks
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[RequireComponent(typeof(Rigidbody2D))]
public class TapController : MonoBehaviour {
public delegate void PlayerDelegate();
public static event PlayerDelegate OnPlayerDied;
public static event PlayerDelegate OnPlayerScored;
public float tapForce = 10;
public float tiltSmooth = 5;
public Vector3 startPos;
Rigidbody2D rigidbody;
Quaternion downRotation;
Quaternion forwardRotation;
void Start() {
rigidbody = GetComponent<Rigidbody2D>();
downRotation = Quaternion.Euler(0, 0, -90);
forwardRotation = Quaternion.Euler(0, 0, 35);
}
void Update() {
if (Input.GetMouseButtonDown(0)) {
transform.rotation = forwardRotation;
rigidbody.velocity = Vector3.zero;
rigidbody.AddForce(Vector2.up * tapForce, ForceMode2D.Force);
}
transform.rotation = Quaternion.Lerp(transform.rotation, downRotation, tiltSmooth * Time.deltaTime);
}
void OnTriggerEnter2D(Collider2D col) {
if (col.gameObject.tag == "ScoreZone") {
//register a score event
OnPlayerScored(); //event sent to GameManager;
//play a sound
}
if (col.gameObject.tag == "DeadZone") {
rigidbody.simulated = false;
//register a dead event
OnPlayerDied(); //event sent to GameManager;
//play a sound
}
}
}
Comment