Trying to reference a variable from a different script but getting error.

NullReferenceException: Object reference not set to an instance of an object
SlimeBallBehavior.Update () (at Assets/Scripts/SlimeBallBehavior.cs:18)

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class SlimeBallBehavior : MonoBehaviour {
    private Rigidbody2D RigidSlimeBall;
    public float speed;
    public float verticality;
    private Slime reference;

	void Start () {
        RigidSlimeBall = GetComponent<Rigidbody2D>();
    }


    void Update()
    { 
        if (reference.slimeReference == false)
        {
            RigidSlimeBall.velocity = new Vector2(speed * Time.deltaTime, verticality);
        }
        if (reference.slimeReference == true)
        {
            RigidSlimeBall.velocity = new Vector2(-speed * Time.deltaTime, verticality);
        }

    }
}

You never set “Slime reference” but you try to use it. You need to set it first, there is a bunch of ways to do this. Maybe start with making this field public and attach your Slime to this script in the inspector.

ok, so the problem was that the Slime Ball Behavior script was attached to a prefab, so I could only attach prefab objects to it in the inspector. I just made my Slime object into a prefab and all is well now :slight_smile: