Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Resolved NullReferenceException

Discussion in 'Scripting' started by Erus47, Jun 19, 2022.

  1. Erus47

    Erus47

    Joined:
    Jun 19, 2022
    Posts:
    3
    Hi i'm new in making game
    i use unity for create my game and visual studio for my code and i have this message
    NullReferenceException: Object reference not set to an instance of an object
    Enemy.Update () (at Assets/Enemy.cs:17)
    Can you help me please

    I join my lign of code


    using UnityEngine;
    public class Enemy : MonoBehaviour {
    public float speed = 10f;
    private Transform target;
    private int waypointIndex = 0;
    void start()
    {
    target = waypoints.points[0];
    }
    private void Update()
    {
    Vector3 dir = target.position - transform.position;
    transform.Translate(dir.normalized * speed * Time.deltaTime, Space.World);
    }
    }


    Sorry for my bad english
     
  2. BABIA_GameStudio

    BABIA_GameStudio

    Joined:
    Mar 31, 2020
    Posts:
    491
    It is because it cannot find a value for target, and the reason it cannot find a value is because you have put
    void start()
    instead of
    void Start()

    C# is case-sensitive, so you must make sure that everything is spelled correctly and with the right capitalisation.
    Once you fix that, I expect you will get another error about
    target = waypoints.points[0];
    because nowhere in your script have you set up an array called
    waypoints
     
  3. Erus47

    Erus47

    Joined:
    Jun 19, 2022
    Posts:
    3
    thank you for your responce i do that and know everything it's good ahah :)