I’m trying to access a variable from a script and it comes back as null reference exception every time, ive tried accessing this exept variacle 50 different ways but get the same thing back everytime, addcomponent and FindObjectOfType get me the same result. Here is my code, h is just from my character controller script which i will show after the counter script
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class Counter : MonoBehaviour {
private Text numeber;
private Characterbehavior gj;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
numeber.text = gj.candyKainz + "/100";
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Characterbehavior : MonoBehaviour
{
private Animator anim;
public float moveSpeed;
private bool playerMoving;
private Rigidbody2D rigidBoi;
private Vector2 lastMove;
public Vector3 place;
public float life;
private float skeldamage;
private bool hit;
public float hitboxtime;
Transform camera;
private SpriteRenderer facing;
public int candyKainz;
public int player = 1;
// Use this for initialization
void Start()
{
candyKainz = 0;
anim = GetComponent<Animator>();
rigidBoi = GetComponent<Rigidbody2D>();
camera = Camera.main.transform;
}
// Update is called once per frame
void Update()
{
camera.position = Vector3.Lerp(transform.position, place, .15f);
place = new Vector3(transform.position.x, transform.position.y, -10);
playerMoving = false;
if (Input.GetAxisRaw("Horizontal") > 0.5f || Input.GetAxisRaw("Horizontal") < -0.5f)
{
playerMoving = true;
lastMove = new Vector2(Input.GetAxisRaw("Horizontal"), 0f);
rigidBoi.velocity = new Vector2(Input.GetAxisRaw("Horizontal") * moveSpeed, rigidBoi.velocity.y);
}
if (Input.GetAxisRaw("Vertical") > 0.5f || Input.GetAxisRaw("Vertical") < -0.5f)
{
playerMoving = true;
lastMove = new Vector2(0f, Input.GetAxisRaw("Vertical"));
rigidBoi.velocity = new Vector2(rigidBoi.velocity.x, Input.GetAxisRaw("Vertical") * moveSpeed);
}
if (Input.GetAxisRaw("Horizontal") < 0.5f && Input.GetAxisRaw("Horizontal") > -0.5f)
{
rigidBoi.velocity = new Vector2(0f, rigidBoi.velocity.y);
}
if (Input.GetAxisRaw("Vertical") < 0.5f && Input.GetAxisRaw("Vertical") > -0.5f)
{
rigidBoi.velocity = new Vector2(rigidBoi.velocity.x, 0f);
}
anim.SetFloat("MoveX", (Input.GetAxisRaw("Horizontal")));
anim.SetFloat("MoveY", (Input.GetAxisRaw("Vertical")));
anim.SetBool("Moving", playerMoving);
anim.SetFloat("LastMoveX", lastMove.x);
anim.SetFloat("LastMoveY", lastMove.y);
}
void OnTriggerEnter2D(Collider2D other)
{
if(other.gameObject.tag == "candy")
{
candyKainz++;
other.gameObject.GetComponent<Candycaine>().collected();
}
}
}