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. Dismiss Notice

Question how to assign the player as a prefab (C#)

Discussion in 'Scripting' started by Lenticularic, Jul 20, 2021.

  1. Lenticularic

    Lenticularic

    Joined:
    Nov 16, 2020
    Posts:
    46
    I'm trying to assign the player as the variable, but when ever i make the variable public and reference the player prefab, unity throws a null reference exception.

    even when I tried using "FindGameObjectWithTag" it didn't work
    Code (CSharp):
    1. public bool isTargeted;
    2.     GameObject Player;
    3.     void Start()
    4.     {
    5.         timer = lifeTime;
    6.  
    7.         //Player = GameObject.FindGameObjectWithTag("Player");
    8.  
    9.         if(!isTargeted) {
    10.             transform.rotation = Quaternion.Euler(0, 0, rotation);
    11.         }
    12.         if(isTargeted) {
    13.             transform.LookAt(Player.transform);
    14.         }
    15.     }
     
  2. gorbit99

    gorbit99

    Joined:
    Jul 14, 2015
    Posts:
    1,350
    Are you sure this is what gives you the exception? If you have it assigned in the editor (preferably with [SerializeField], not public), then it should work just fine, and if you don't, that's not even a nullref. Can you tell exactly what line this exception is on?
     
    Lenticularic likes this.
  3. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,756
    From your post it is unclear if you intend to publicly define and assign the Player in the editor, or if you are hoping to find it at runtime via some method such as tags or whatever.

    Before you write another character of code, you need to decide on ONE approach. Both approaches will not work well together.

    I suggest the former approach: make it public, drag a reference in, save the scene / prefab... DONE. Never again worry about it.

    For your reference, whenever you see a nullref, there is only ONE process to go through:

    The answer is always the same... ALWAYS. It is the single most common error ever.

    Don't waste your life spinning around and round on this error. Instead, learn how to fix it fast... it's EASY!!

    Some notes on how to fix a NullReferenceException error in Unity3D
    - also known as: Unassigned Reference Exception
    - also known as: Missing Reference Exception
    - also known as: Object reference not set to an instance of an object

    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.

    Here is a clean analogy of the actual underlying problem of a null reference exception:

    https://forum.unity.com/threads/nul...n-instance-of-an-object.1108865/#post-7137032
     
    Last edited: Jul 20, 2021
    Lenticularic likes this.
  4. Lenticularic

    Lenticularic

    Joined:
    Nov 16, 2020
    Posts:
    46
    I got it to work, but now when ever I hit play the objects sprite is gone, and weird stuff happens
    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class LookAtPlayer : MonoBehaviour
    4. {
    5.     public Transform Player;
    6.  
    7.     void Update() {
    8.         transform.LookAt(Player);
    9.     }
    10. }
    I'm so confusion