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

How to add Script to prefab ?

Discussion in 'Android' started by m-y, Jun 15, 2014.

  1. m-y

    m-y

    Joined:
    Sep 22, 2013
    Posts:
    470
    Hello
    i have script
    i use it inside another script
    i can use it when the cube in the scene
    but when i make the cube as prefab
    it is not able to use it
    can i know what is the solution /.?

    that is the code
    i am using in the
    ( Hela Cube )

    var HealthScript : HealthBar1 ;

    pro1.png

    pro2.png
     
  2. DanielQuick

    DanielQuick

    Joined:
    Dec 31, 2010
    Posts:
    3,137
    Prefabs cannot link to instanced GameObjects because they are not guaranteed to be there.

    You will need to link it in the instanced prefab (after you add it to the scene) or assign it via script, likely in the Awake function.
     
  3. m-y

    m-y

    Joined:
    Sep 22, 2013
    Posts:
    470
    how i can write code like that ?
    it is first time to me ? !
     
  4. m-y

    m-y

    Joined:
    Sep 22, 2013
    Posts:
    470
    how i can link to to instanced prefab ?
     
  5. DanielQuick

    DanielQuick

    Joined:
    Dec 31, 2010
    Posts:
    3,137
    It would be what you did in the first image, linking it in the scene.
     
  6. m-y

    m-y

    Joined:
    Sep 22, 2013
    Posts:
    470
    can u say it clear
    i can't get what u mean ?
     
  7. Magiichan

    Magiichan

    Joined:
    Jan 5, 2014
    Posts:
    403
    Code (CSharp):
    1. HealthScript = targetObject.GetComponent(HealthBar1);
    You would have to acquire the targetObject to do this.