Question Instance of object

porkshopp

Active member
Joined
Apr 13, 2019
Messages
26
Location
Sweden
Programming Experience
Beginner
I get these errors while running the code in Unity: "
NullReferenceException: Object reference not set to an instance of an object
MoveGene.Update () (at Assets/MoveGene.cs:87)
"
"
NullReferenceException: Object reference not set to an instance of an object
MoveGene.Start () (at Assets/MoveGene.cs:98)
"

C#:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class MoveGene : MonoBehaviour
{
    public int FoodCap = 1;
public Rigidbody rb;
public GameObject food;
public GameObject Organism;
public float scope = 1f;
public float speed = 1f;
public bool isHome = false;
public Vector3 Home;
public float hRange = 0.5f;
private float startTime;
public float t;
// Start is called before the first frame update
private Consume consume;
void Awake()
{
consume = GetComponent<Consume>();
    }
    void Start()
{
consume.fed = 0;
startTime = Time.time;
Home = rb.position;

       
    }




     

    // Update is called once per frame
void Update()
{
while ( Organism != null)
        {

        }

         t = (Time.time - startTime);
if (food == null)
{
           
            float n = (rb.transform.position.z - Home.z) / (rb.transform.position.x - Home.x);
            if (rb.transform.position.x > Home.x && isHome == false) rb.AddForce(-speed * Time.deltaTime, 0, -n * speed * Time.deltaTime);
            else if (rb.transform.position.x < Home.x && isHome == false) rb.AddForce(speed * Time.deltaTime, 0, n * speed * Time.deltaTime);
            if ((Mathf.Abs(rb.transform.position.z - Home.z) < hRange && Mathf.Abs(rb.transform.position.x - Home.x) < hRange))
{
rb.velocity = Vector3.zero;
isHome = true;
}




}
if (food != null)
{

if (Mathf.Abs(rb.transform.position.x - food.transform.position.x) < scope && Mathf.Abs(rb.transform.position.z - food.transform.position.z) < scope)
            {

float k = (rb.transform.position.z - food.transform.position.z) / (rb.transform.position.x - food.transform.position.x);
if (rb.transform.position.x > food.transform.position.x) rb.AddForce(-speed*Time.deltaTime, 0, -k * speed*Time.deltaTime);
                else rb.AddForce(speed*Time.deltaTime, 0, k * speed*Time.deltaTime);

               

            }

}
if (t > 2)
{
if (consume.fed > 1 && isHome == true)
{


}
else Destroy(gameObject);
}
Debug.Log("ffff");
}
}

C#:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Consume : MonoBehaviour
{
public int fed = 0;
void OnTriggerEnter(Collider other)
{
if (other.CompareTag("Player"))
{
Pickup();
}
    }
    void Pickup()
{
Destroy(gameObject);
fed++;
}
}

How can I use the variable fed from Consume in MoveGene?
 
when I pick up the consumable I want the character to be fed. I don't know were specifically but does it matter where? Is it a different line of code if I have it on the top rather than the bottom?
 
If you don't know where you expect that variable to be assigned a value then it is no surprise that no valid is assigned. It sounds like you need to spend sometime getting a better understanding of what you're actually trying to achieve.
 

Latest posts

Back
Top Bottom