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
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

How to solve the problem with null and transform?[SOLVED]

Discussion in 'Scripting' started by polan31, Sep 22, 2018.

  1. polan31

    polan31

    Joined:
    Nov 20, 2017
    Posts:
    149
    Good morning everyone.

    I wrote a script.

    I use it for 2-3 days.

    Everything was ok and worked great.

    However,today, when I launched Unity, the following errors appear to me:

    NullReferenceException: Object reference not set to an instance of an objectPathFollower1.Update () (at Assets/PathFollower1.cs:26)

    UnityException: Transform child out of bounds PathFollower1.Start () (at Assets/PathFollower1.cs:20)

    Does anyone know what is wrong? Why I have mistakes and how to solve it?

    My script:

    Code (CSharp):
    1.  using System.Collections;
    2.    using System.Collections.Generic;
    3.    using UnityEngine;
    4.  
    5.    public class PathFollower1 : MonoBehaviour {
    6.  
    7.  
    8.      public float speed = 3f;
    9.      public Transform pathParent;
    10.      Transform targetPoint;
    11.  
    12.      int index;
    13.      public float speedRotate = 7f;
    14.      Vector3 distance;
    15.  
    16.      Quaternion newRotate;
    17.  
    18.      // Use this for initialization
    19.      void Start () {
    20.          index = 0;
    21.          targetPoint = pathParent.GetChild (index);
    22.      }
    23.      // Update is called once per frame
    24.      void Update () {
    25.          transform.position = Vector3.MoveTowards (transform.position, targetPoint.position, speed * Time.deltaTime);
    26.          rotate ();
    27.          if (Vector3.Distance (transform.position, targetPoint.position) < 0.1f)
    28.          {
    29.              index++;
    30.              index %= pathParent.childCount;
    31.              targetPoint = pathParent.GetChild (index);
    32.          }
    33.      }
    34.      void rotate ()
    35.      {
    36.          distance = transform.position - targetPoint.position;
    37.          newRotate = Quaternion.LookRotation (distance, transform.forward);
    38.          newRotate.x = 0;
    39.          newRotate.y = 0;
    40.          transform.rotation = Quaternion.Lerp (transform.rotation, newRotate, speedRotate * Time.deltaTime);
    41.      }
    42. }
     
  2. Diablo404

    Diablo404

    Joined:
    Mar 15, 2013
    Posts:
    136
    Maybe you didn't save you scene when you have quitted Unity.

    public Transform pathParent is probably not set ( drag'n'drop ) in the Editor anymore
     
  3. polan31

    polan31

    Joined:
    Nov 20, 2017
    Posts:
    149
    I save and also added in the inspector.
    PS: In an object that after pressing play becomes a parent object, I have such a script:

    Code (CSharp):
    1.  using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class PathPlacer : MonoBehaviour {
    6.  
    7.      public float spacing = .1f;
    8.      public float resolution = 1;
    9.      public GameObject someObject;
    10.  
    11.      void Start () {
    12.          Vector2[] points = FindObjectOfType<PathCreator>().path.CalculateEvenlySpacedPoints(spacing, resolution);
    13.  
    14.          foreach (Vector2 p in points)
    15.  
    16.          {
    17.              GameObject g = GameObject.CreatePrimitive(PrimitiveType.Sphere);
    18.              g.transform.parent = someObject.transform;
    19.              g.transform.position = p;
    20.              g.transform.localScale = Vector3.one * spacing * .5f;
    21.          }
    22.      }
    23. }
     
  4. polan31

    polan31

    Joined:
    Nov 20, 2017
    Posts:
    149
    Ok, I restarted and it works again ...