So I’m trying to make it when your character stands on a certain thing, in this case a yellow pad with the tag jumpboost, your character gets some affect. There isn’t any syntax error but when I start the game it spams in the console “NullReferenceException: Object reference not set to an instance of an object PlayerMotor.GiveJumpBoost () (at Assets/Scripts/PlayerMotor.cs:66)” Can anyone help me? The error is on line 66.
using UnityEngine;
using System.Collections;
[RequireComponent(typeof(Rigidbody))]
[RequireComponent(typeof(GameObject))]
public class PlayerMotor : MonoBehaviour {
private Vector3 velocity = Vector3.zero;
[SerializeField]
private float jumpSpeed = 8f;
public float jumpBoostStrength = 10f;
public float speedBoostStregth = 8f;
private Rigidbody rb;
void Start()
{
rb = GetComponent<Rigidbody>();
}
//Gets a movement vector
public void Move(Vector3 _velocity)
{
velocity = _velocity;
}
//Run every physics iteration
private void FixedUpdate()
{
PerformMovement();
PerformJump();
GiveJumpBoost();
}
//Perform movement based on velocity variable
void PerformMovement()
{
if (velocity != Vector3.zero) //Time.fixedDeltaTime is the time between each frame
{ //This prevents someone from moving faster on higher fps
rb.MovePosition(rb.position + velocity * Time.fixedDeltaTime);
}
}
//Jump based on a velocity variable
void PerformJump()
{
//Vector3 variable
Vector3 dwn = transform.TransformDirection(Vector3.down);
if (Physics.Raycast(transform.position, dwn, 0.6f))
{
if (Input.GetButtonDown("Jump"))
rb.velocity = new Vector3(0, jumpSpeed, 0);
}
}
void GiveJumpBoost()
{
Vector3 dwn2 = transform.TransformDirection(Vector3.down);
RaycastHit rayHit = new RaycastHit();
if (Physics.Raycast(transform.position, dwn2, 1f))
{
Collider raycastHitCollider = rayHit.collider;
if (raycastHitCollider.gameObject.tag == "jumpboost")
{
Debug.Log("JUUMP!!!!");
}
}
}
}