Search Unity

problem setting a position in script

Discussion in 'Scripting' started by Robster95, Dec 3, 2020.

  1. Robster95

    Robster95

    Joined:
    Jul 3, 2019
    Posts:
    154
    I am trying to set a private transform position to a gameobject that will be destroyed but I am unable to figure out how to do so.

    Everything I try comes back as a "Object reference not set to an an instance of an object" error.

    I and finding an actve object in the scene and setting the transform (stated above) to the object found in the start method by doing

    Code (csharp):
    1.  
    2.             shovelSpot.position = GameObject.Find("Shovel").transform.Position;
    3.  
    if anyone can help with this that would be greatly appreciated!
     
  2. Vryken

    Vryken

    Joined:
    Jan 23, 2018
    Posts:
    2,106
    Either there's no GameObject named "Shovel" in your scene, or it's disabled when you try finding it.
    GameObject.Find
    only returns objects that are enabled.

    You also have a typo at the end of the line there; it should be "transform.position", not "transform.Position".
     
  3. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,742
    This is ALWAYS the same answer, ALWAYS. Learn to fix it fast so you don't waste your time on trivial errors.

    Some notes on how to fix a NullReferenceException error in Unity3D
    - also known as: Unassigned Reference Exception
    - also known as: Missing Reference Exception

    http://plbm.com/?p=221

    The basic steps outlined above are:
    - Identify what is null
    - Identify why it is null
    - Fix that.

    Expect to see this error a LOT. It's easily the most common thing to do when working. Learn how to fix it rapidly. It's easy. See the above link for more tips.

    This is the kind of mindset and thinking process you need to bring to this problem:

    https://forum.unity.com/threads/why-do-my-music-ignore-the-sliders.993849/#post-6453695

    Step by step, break it down, find the problem.