Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • 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.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

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

[SOLVED] Prefab as copy not as reference 1 Answer

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

Problem passing a GameObject to another script 0 Answers

How do I reference any instance of a prefab? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges