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

[HElLP] How to access the components of a clone prefab

Discussion in 'Scripting' started by Pavlos_Mavris, Mar 15, 2020.

  1. Pavlos_Mavris

    Pavlos_Mavris

    Joined:
    Sep 17, 2018
    Posts:
    57
    Hello everyone,

    I have a question about prefabs. I'm trying to access the component of multiple clones prefabs that I have instantiate but I can't.
    I'm using this line of code

    Code (CSharp):
    1. GameObject newpipe = Instantiate(pipe) as GameObject;
    2. MovePipes cloneScript = newpipe.GetComponent<MovePipes>();
    and with "cloneScript" I can control the script that is assigned into the prefab, but my issue is that any changes I do are happening for the clone prefabs that will be instantiate later, but I want the changes to happen to the prefabs that already created.

    Let's assume that 3 clone prefabs was created and after a few seconds I want to make some changes to those clones, how can I get access to those 3?
     
    lincolnsupermen likes this.
  2. Nyfirex

    Nyfirex

    Joined:
    Jun 26, 2019
    Posts:
    179
    GameObject newpipe is a reference. When you instantiate the object newpipe point to that object so you if you want to access to its components you have to write newpipe = GetComponent<MovePipes>();
     
  3. Pavlos_Mavris

    Pavlos_Mavris

    Joined:
    Sep 17, 2018
    Posts:
    57
    First I want to thank you for replying,

    This works for every prefab that is going to be instantiated, but in my case I want to make changes to all the clone objects that are created. An example is that I created 3 clone objects and when the specific command will be executed the changes will not be applied to those 3 clone objects, it will be executed to those that are going to be created, but I want it to execute the command into those 3 clone objects that already created.
     
  4. Nyfirex

    Nyfirex

    Joined:
    Jun 26, 2019
    Posts:
    179
    For that i would use a function inside the prefab. Like SetStats(parameters) then you instantiate your objects doing newpipe = GetComponent<MovePipes>(); and newpipe<GetComponent<MovePipes>().SetStats(parameters)
    So when you want to modify just change the parameters and not the actual prefab stats
     
  5. In0sc0p3dJFK

    In0sc0p3dJFK

    Joined:
    May 14, 2020
    Posts:
    2
    I know this is late but:
    You can just use a tag. Drag the object that is going to be cloned into the Project to make it a prefab first. Delete it from the world space since its going to be instantiated using the prefab. Then make a tag with your objects name "Object". Make sure you remember it because it's case-sensitive. Then, in your object prefab, give it the tag you just made. And in anything that was using the Object as a reference to its Transfrom or GameObject, just drag the prefab into the slot because i'm assuming you were just using the world space version of the object.

    Then in a script (it can be attached to anything), Get the component of the Tagged objects.

    Code (CSharp):
    1. public string objectTag = "ObjectName"
    2.  
    3. void Start () //or any other method
    4. {
    5.       GameObject.FindWithTag(objectTag).gameObject. <anyComponent>();
    6.  
    7. }
    You don't have to make a String for the tag. In the "FindWithTag()" you can just put quotes with its name in it; ...FindWithTag("ObjectName")

    if you want to get a component such as a bool or int from a script of that object, then you would simply just do:

    Code (CSharp):
    1. public string objectTag = "ObjectName"
    2.  
    3. void Start () //or any other method
    4. {
    5.       GameObject.FindWithTag(objectTag).gameObject. <anyComponent>(). componentWithinScript;
    6.  
    7. }
    Make sure you give the set the Object prefab's tag, and fill in any missing references with the prefab, not the world space version of it.

    Although, if you want to change the component of only that specific clone, it's a little different.
     
    Last edited: May 15, 2021
  6. abhin7993

    abhin7993

    Joined:
    Aug 23, 2019
    Posts:
    7
    Hello Everyone,

    I am facing the same problem in Prefab Clone, but the situation is slightly different, as here i need to store a value in the components (marked with yellow tick) of the PrefabClone(marked with green tick), and as another PrefabClone builds so i need to update its component with another value.

    So, is there any way that i can be able to do such a case,
    Any help will be greatly appreciated..
    upload_2022-5-21_9-7-38.png
     
  7. FunnyVanya

    FunnyVanya

    Joined:
    Feb 4, 2022
    Posts:
    1
    Hi! The same q. Have you solution? thx
     
  8. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,971
    Please don't necro post with vague questions. Instead, start your own fresh post... it's FREE!

    When you post, remember we cannot read your mind so you MUST communicate.

    How to report your problem productively in the Unity3D forums:

    http://plbm.com/?p=220

    This is the bare minimum of information to report:

    - what you want
    - what you tried
    - what you expected to happen
    - what actually happened, especially any errors you see
    - links to documentation you used to cross-check your work (CRITICAL!!!)

    If you post a code snippet, ALWAYS USE CODE TAGS:

    How to use code tags: https://forum.unity.com/threads/using-code-tags-properly.143875/