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

Can't Seem to Assign World Variable Here

Discussion in 'Scripting' started by vcinardo, Jun 28, 2022.

  1. vcinardo

    vcinardo

    Joined:
    Dec 23, 2019
    Posts:
    27
    I have a single GameObject called "Earth" that has all my assets on it. I am trying to spawn in a prefab which has a script, whose only variable is "earth". I cannot seem to drag in the Earth object into the saved prefab's script in the project explorer.

    So I tried solving this with scripting. This is the script of the prefab that is supposed to spawn in...

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Obstacle : MonoBehaviour
    6. {
    7.     public GameObject earth;
    8.  
    9.     void Start()
    10.     {
    11.         earth = GameObject.Find("Earth");
    12.     }
    13.  
    14.     private void OnTriggerEnter(Collider other)
    15.     {
    16.         earth.GetComponent<Spin>().StopWorld();
    17.         other.GetComponent<BadDogControls>().Kill();
    18.         other.GetComponent<Animator>().Play("Arm_Dog|Death_2");
    19.         //other.GetComponent<Animation>().Play(anim.toString());
    20.     }
    21.  
    22. }
    23.  
    My error message:
    UnassignedReferenceException: The variable earth of Obstacle has not been assigned.
    You probably need to assign the earth variable of the Obstacle script in the inspector.
    UnityEngine.GameObject.GetComponent[T] () (at <f1212ad1dec44ce7b7147976b91869c3>:0)
    Obstacle.OnTriggerEnter (UnityEngine.Collider other) (at Assets/Scripts/Obstacle.cs:16)

    I cannot seem to find the solution online, any help would be appreciated.
     
  2. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,742
    GameObject.Find is terrible, and any tiny issue with it (a typo, trailing space, or miscapitalization in the name, among others) will make it fail with no clues. Here's a link with explanations and many alternatives - in this case, the singleton is probably what you want.
     
  3. SimonJ9

    SimonJ9

    Joined:
    Feb 5, 2018
    Posts:
    17
    Correct me if I'm wrong. It looks like the "Earth" object you mentioned is an object in your scene hierarchy, and you are trying to drag that onto a prefab on your hard drive. That won't work, because your Obstacle prefab is not guaranteed to have access to your Earth object in this specific scene. You can either:
    1. Save the Earth object as another prefab, then assign this prefab to you Obstacle.earth, or
    2. Put your Earth object as a child object of you Obstacle, and save the Obstacle prefab. In your script you can do something like
    Code (CSharp):
    1. public GameObject earth { get { return transform.GetChild(0).gameObject; } }