Getting all gameobjects and creating only their spriterenderer components;

Hi ım trying to make a reflection effect and ım getting all gameobjects and creating theirs spriterenderer components but it is copying directly themselfs.

 void Start()
    {
       
    }

    // Update is called once per frame
    void Update()
    {
      if(Input.GetKeyDown(KeyCode.Q))  mirror();
    }

    void mirror()
    {
        GameObject[] go = GameObject.FindObjectsOfType<GameObject>();

        SpriteRenderer[] eben=new SpriteRenderer[go.Length];

        for (int i=0;i< go.Length; i++)
        {
            float yy = go[i].transform.position.y - transform.position.y;

            if (go[i].GetComponent<SpriteRenderer>() != null)

            {
               eben[i] = go[i].GetComponent<SpriteRenderer>();

                Instantiate(eben[i], new Vector3(
                    eben[i].transform.position.x,
                    transform.position.y - yy,
                    eben[i].transform.position.
                z), Quaternion.identity);

            }
               


        }
      

    }

Instantiate copies the entire GameObject. That’s what its purpose is. If you just want the sprite, just instantiate a prefab that has only a SpriteRenderer, and assign the sprite from the original to that new Renderer.

ım creating a gameobject array and store the gameobjects then ım creating spriterenderer arrays then ım attaching gameobjects spriterenderers .Am I not making the same thing you said

I know what you are doing. I can see your code. When you call Instantiate on any component of a GameObject (such as the SpriteRenderer), it copies the entire GameObject including all of its components, and child GameObjects etc.

I’m telling you that is how it works, whether you like it or not. So you should not use Instantiate on the existing object if you want to only copy the SpriteRenderer.

2 Likes

But ı am not calling Instantiate on gameobject ım calling it on new gameobject that has only spriterenderer.You are saying that if I Instantiate a gameobject it will create all of its components but ı am not instatiante the gameobject.

SpriteRenderer InstantiateThis =Gameobject.GetComponent<SpriteRenderer>();

above this code instantiateThis only has spriterenderer component right? so ı am just instantiante a gameobject that has only spriterenderercomponent

That is not what your code is doing.

Your code is getting all GameObjects that have a SpriteRenderer:

GameObject[] go = GameObject.FindObjectsOfType<GameObject>();

Then it’s grabbing the SpriteRenderer from each of those objects:
eben[i] = go[i].GetComponent<SpriteRenderer>();and instantiating them:Instantiate(eben[i], ...)

These are not “new GameObject that has only SpriteRenderer”. These are all the GameObjects in your scene with SpriteRenderers, ragardless of what other components they also have.

No it has whatever components it has. There is no indiciation in this code of what components that GameObject has. It can have any number of other components.

1 Like

how can ı do that than

I already mentioned a solution above:

1 Like

thank you

it works

 GameObject[] go;
    public GameObject an;
    GameObject[] hey;
    void Start()
    {
      go = GameObject.FindObjectsOfType<GameObject>();
        hey = new GameObject[go.Length];
        mirror();
    }

    // Update is called once per frame
    void Update()
    {
   
        miron();
    }

    void mirror()
    {
      

     
     
        for (int i = 0; i < go.Length; i++)
        {

           // eben[i].AddComponent<SpriteRenderer>();
        }

            for (int i=0;i< go.Length; i++)
        {
            float yy = go[i].transform.position.y - transform.position.y;

            if (go[i].GetComponent<SpriteRenderer>() != null)

            {
          

              hey[i]=  Instantiate(an, new Vector3(
                    go[i].transform.position.x,
                   transform.position.y - yy,
                    go[i].transform.position.
                z), Quaternion.identity);

            }
               


        }
      

    }

    void miron()
    {
        for (int i = 0; i < go.Length; i++)
        {

          if(go[i].GetComponent<SpriteRenderer>()!=null)  hey[i].GetComponent<SpriteRenderer>().sprite = go[i].GetComponent<SpriteRenderer>().sprite;
        }
    }