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. Voting for the Unity Awards are OPEN! We’re looking to celebrate creators across games, industry, film, and many more categories. Cast your vote now for all categories
    Dismiss Notice
  3. Dismiss Notice

Can't get a prefab to update

Discussion in 'Editor & General Support' started by dusthound, Oct 11, 2018.

  1. dusthound

    dusthound

    Joined:
    Sep 18, 2018
    Posts:
    98
    I am trying to make changes to a prefab using an instance of the prefab but it is not working. What I am trying to do is add a reference to a text object. I have created a variable(text) and put the text object into the variable slot in the inspector. After that I hit apply but the prefab doesn't have the variable in the slot. It just says that there is no gameobject in the slot.
     
  2. mark_cb

    mark_cb

    Joined:
    Jun 9, 2015
    Posts:
    17
    Is the text object a child of the prefab (because it needs to be for the reference to work)? My suspicion is that you're trying to reference something else in the scene but a prefab cannot store scene references in that way. The reason being, imagine you loaded that prefab into a completely different scene, that reference would be invalid, thus it isn't allowed.
     
  3. dusthound

    dusthound

    Joined:
    Sep 18, 2018
    Posts:
    98
    The text object is not a child of the prefab. How do I make it a child?
     
  4. dusthound

    dusthound

    Joined:
    Sep 18, 2018
    Posts:
    98
    And yes, it is an object in the scene I am currently working on.
     
  5. APSchmidt

    APSchmidt

    Joined:
    Aug 8, 2016
    Posts:
    4,459
    Does the problem persist if you reload the project?

    Sometimes, the best is to delete the prefab and remake it entirely by dragging the instance into the prefab folder. :)
     
  6. mark_cb

    mark_cb

    Joined:
    Jun 9, 2015
    Posts:
    17
    If you're using a text object, that should already be a child of a canvas (because it needs to be). So to make it a child of your prefab you'd need to drag the canvas onto your prefab instance in the hierarchy. If this is a small text label that renders in the world that should be ok.

    However, if your text is part of something which you want only one of (let's say it's a HUD) then the issue gets a little more complicated. You don't want every prefab instance bringing a new hud with it. In that case I'd leave the canvas in the scene and add a tag to it labelled as "hud". Then in your prefab that wants to reference that have a call in the awake function of your script use GameObject.FindWithTag("hud") to get a reference to the hud within your scene. As with a lot of coding, this isn't necessarily the fastest or smartest way to do it, but it works while you're learning.
     
  7. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    You can't reference anything in a scene via an inspector field on a prefab. As already mentioned you should establish your reference at run time.
     
  8. dusthound

    dusthound

    Joined:
    Sep 18, 2018
    Posts:
    98
    I have tried using GameObject.FindObjectWithTag, but the object I am trying to reference is a UI element, so it get an error saying that i can't convert from a UI element to a GameObject.
     
  9. mark_cb

    mark_cb

    Joined:
    Jun 9, 2015
    Posts:
    17
    You can then find the UI component on that GameObject using GetComponent
     
  10. dusthound

    dusthound

    Joined:
    Sep 18, 2018
    Posts:
    98
    Thanks, i will try that