I am new to Unity and I keep getting this error, “Object reference not set to an instance of an object”
Here are my two scripts.
using UnityEngine;
using System.Collections;
public class CameraController : MonoBehaviour
{
float height;
public GameObject theBall;
float topJump;
private JumpScript JumpScript;
void Start()
{
height = transform.position.y;
}
void Update()
{
JumpScript = theBall.GetComponent();
topJump = JumpScript.topJump;
if (topJump > height)
{
height = transform.position.y;
transform.position = new Vector2(0F, height);
}
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class JumpScript : MonoBehaviour
{
[Range(1, 10)]
public float jumpVelocity;
public float topJump= 0.0F;
void Update()
{
if (transform.position.y >= topJump)
{
topJump = transform.position.y;
}
if (Input.GetKeyDown(“space”))
{
GetComponent().velocity = Vector2.up * jumpVelocity;
}
}
}
The issue seems to be gathering the variables data from JumpScript but idk why it happens.
Welcome to the Unity forums!
A few thing to help you out to get faster and more useful responses:
A) post in the correct forum. (I moved this to “Scripting”)
B) __ use code tags. __
C) When you have errors like that, it will tell you exactly what line it is on. Look at the error and check what line number. Also, typically double clicking on the error will take you the exact spot in your text editor.
Thank you sm! I found the exact line but idk how to fix it. The line is topJump = JumpScript.topJump;
There is an issue finding the value or something idk.
Your error indicates that the JumpScript property in your CameraController is null. This means that the code theBall.GetComponent<JumpScript>();
is not finding a JumpScript component on your theBall
object. Are you sure the JumpScript is on the ball? Note that it has to be on the same gameObject, not a child game object. If JumpScript is on a child of the ball, you’d use GetComponentInChildren instead.
Also, it’s generally not a good idea to call GetComponent within Update, as it’s fairly expensive. It’s better to call it once in the Start method.
Thank you so much. I changed the code to just check the y coordinate of the game object PlayerBall. However, now I can’t see anything in my game but on the scene it looks like it’s working.
using UnityEngine;
using System.Collections;
public class CameraController : MonoBehaviour
{
float height;
public GameObject PlayerBall;
void Start()
{
height = 0;
}
void Update()
{
if (PlayerBall.transform.position.y > transform.position.y)
{
height = PlayerBall.transform.position.y-transform.position.y;
}
transform.position = new Vector2(0F, transform.position.y+height);
height = 0;
}
}
The original coordinates of my camera are 0,0,-1. If I disable the script then I can see stuff again but my camera doesn’t raise as the object reaches a peak in height.
Nvm I fixed it by setting it to a Vector3 and making the z coordinate to -1. For some reason using Vector2 switches the Z coordinate back to 0 and I couldn’t see other stuff.