• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Lyonsbanner · Mar 17, 2015 at 09:00 PM · prefabsreferenceanother script

How to put a reference on a prefab through the script ?

Hello,

I pick the same example as this tutorial : Unity - GetComponent

On difference, I add a List<>.

 public class AsecondaryScript : MonoBehaviour
 {
     public int maVariable = 120;
 
     public List<string> testList = new List<string>();
 
     void Start ()
     {
         testList.Add ("Objet A 1");
         testList.Add ("Objet A 2");
         testList.Add ("Objet A 3");
     }
 }


I link this script on a GameObject named : Pair1-1

Next, I put this script on another GameObject named : Pair1-2

 public class AmainScript : MonoBehaviour
 {
     public GameObject otherGameObject;
 
     private AsecondaryScript anotherScript;
 
     void Awake()
     {
         anotherScript = otherGameObject.GetComponent<AsecondaryScript> ();
     }
 
     IEnumerator Start ()
     {
         yield return new WaitForEndOfFrame ();
 
         Debug.Log ("Result 1 : " + anotherScript.maVariable);
         Debug.Log ("Result 2 : " + anotherScript.testList);
         Debug.Log ("Result 3 : ");
 
         foreach(String m in anotherScript.testList)
         {
             Debug.Log( "->" + m);
         }
     }
 }


And finally, I put the reference GameObject Pair1-1 in "OtherGameObject" and it work.

The problem is, if both of my GameObject are Prefabs and are not put in the interface, I can't refer Pair1-1 to Pair1-2.

I need to do it with script.

But how could I do this ?

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer Wiki

Answer by salex100m · Mar 18, 2015 at 12:34 AM

when the prefab is Instantiated, it is "awoken" so you can do this (attach the apple to one gameobject, and the banana to another) :

 using UnityEngine;
 using System.Collections;
 
 public class AppleScript : MonoBehaviour {
 
 
     public GameObject theGOwithBanana;
 
     void Awake(){
         theGOwithBanana = GameObject.Find ("GameObject1");
         
     }
 
 }
 
 
 using UnityEngine;
 using System.Collections;
 
 public class BananaScript : MonoBehaviour {
     
     
     public GameObject theGOwithApple;
     
     void Awake(){
         theGOwithApple = GameObject.Find ("GameObject2");
         
     }
     

If the game object already exists in the game (not instantiated at run time), this will also work. Also, don't use "Find" anywhere other than Awake/Start.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Lyonsbanner · Mar 18, 2015 at 01:36 PM 0
Share

Thanks. ;)

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Prefab, referencing them, using their methods:bug or intentional? 1 Answer

How to assign prefab at runtime? 1 Answer

When attempting to call a method from within another script I get this error. 3 Answers

Are sprites reference (script) loaded every time? 0 Answers

How to shoot a bullet based on another object's position? 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges