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

How to access a script on a prefab clone

Discussion in '2D' started by KevG, Feb 28, 2016.

  1. KevG

    KevG

    Joined:
    Oct 4, 2015
    Posts:
    20
    Hi

    I have written is simple script that takes one prefab ( simple 32 x 32 pixel square ) and creates a tilemap of 25 clones of this single prefab, ( 5 x 5 ) tilemap. This works perfect

    Attached to the prefab is a single script that holds 2 variables, let's say x & y, therefore each clone would have a copy of the script.

    so my question is how can I access the individual script of the clones at run time.

    TIA
     
  2. LiterallyJeff

    LiterallyJeff

    Joined:
    Jan 21, 2015
    Posts:
    2,802
    I'm assuming you're using Instantiate for the clone-making.

    You just need a reference to the game object, which has the function GetComponent.

    So for your spawning loop, it would just be something like this:
    Code (CSharp):
    1. for(int i = 0; i < numclones; i++){
    2.     GameObject clone = Instantiate(prefabReference);
    3.     CloneScript cloneScript = clone.GetComponent<CloneScript>();
    4. }
     
    Sk4z0cHNlk and KevG like this.
  3. KevG

    KevG

    Joined:
    Oct 4, 2015
    Posts:
    20
    Thanks jeffreyschoch

    I'll give that a go.
     
  4. ShokeR0

    ShokeR0

    Joined:
    Feb 24, 2016
    Posts:
    112
    Gameobject[] tilemap
    Inside the loop you add all the clones to the array.
    Or you can use findallwithtag .
    I am writing this on my mobile so can't write in much details sorry
     
  5. KevG

    KevG

    Joined:
    Oct 4, 2015
    Posts:
    20
    Code (CSharp):
    1.  
    2. for(int i = 0; i < numclones; i++){
    3.     GameObject clone = Instantiate(prefabReference);
    4.     CloneScript cloneScript = clone.GetComponent<CloneScript>();
    5. }

    Jeffreychoch

    Sorry to be a pain, but this must be me being dumb

    With your code above what is the prefabReference ??, how do I get it, ??
     
  6. LiterallyJeff

    LiterallyJeff

    Joined:
    Jan 21, 2015
    Posts:
    2,802
    In the class, you'll need a public GameObject variable, in my example code the variable would be called "prefabReference". Public fields, or fields with the [SerializeField] tag will show up in the inspector on the component. So you can then drag your prefab from the project files to the component, and then when the game runs that component will have the reference to the prefab.
     
    KevG likes this.
  7. KevG

    KevG

    Joined:
    Oct 4, 2015
    Posts:
    20
    Doh

    I get it now.

    I wasn't dragging the prefab.

    Thanks once again jeffreyschoch
     
    LiterallyJeff likes this.