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

Add GameObject to the Script of a Prefab

Discussion in 'Scripting' started by adri1992, Jan 24, 2016.

  1. adri1992

    adri1992

    Joined:
    Dec 14, 2015
    Posts:
    34
    Hi everyone,

    I need to create a prefab of four spheres. These spheres are falling down from the top of the scene, and they should move to another invisible sphere (through the MoveTowards method).

    For this, I've made a script that is attached to each one of the four spheres, that needs these GameObjects:

    - The sphere (itself).
    - The target sphere (the final position).

    There isn't problem with attaching the first GameObject mentioned, but the target spehre is impossible to attacht it on the script of the prefab... I can't do that =(

    How can I do this?

    Thanks so much
     
  2. Glockenbeat

    Glockenbeat

    Joined:
    Apr 24, 2012
    Posts:
    669
    Hard to tell without any code. However the idea should be to have a public variable on your script. When instantiating the sphere get a reference to the script within the script which is responsible for instantiating. Then with that reference you haev access to any public property, method and so on and may simply assign the target sphere to the public variable within the script.