• 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 GeorgeLuther · Dec 05, 2017 at 05:43 AM · instantiateinstanceinstantiate prefabaccessing scriptsunique

Accessing variable in a specific instance of a prefab

I have a script on Object_A called script_A. It instantiates instances of prefab Object_B which has script on it called script_B. This script contains an public integer variable called randomNumber.


Let’s say I’ve created 3 instances of Object_B and each instance’s randomNumber is unique.

How to I retrieve each specific instance’s unique randomNumber in script_A?


Here's script_A:


 public Rigidbody Object_B;
 public int randomNumber1;
 public int randomNumber2;
 public int randomNumber3;

 void Update ()
 {
     if (Input.GetButtonDown ("Fire1")) 
     //on left click, instantiate an instance of Object_B at Object_A's location+rotation
     {
         Rigidbody Object_B_Instance;
         Object_B_Instance = Instantiate (Object_B, transform.position, transform.rotation) as Rigidbody;
     }
     randomNumber1 = GameObject.Find("Object_B (clone)").GetComponent<script_B>().randomNumber;
     randomNumber2 = GameObject.Find("Object_B (clone)").GetComponent<script_B>().randomNumber;
     randomNumber3 = GameObject.Find("Object_B (clone)").GetComponent<script_B>().randomNumber;
 }

Here's script_B


 public int randomNumber;

 void Start ()
 {
     randomNumber = Random.Range (1, 10);
 }
Comment
Add comment · Show 1
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 Nikhil12 · Dec 05, 2017 at 08:20 AM 0
Share

I dont know if it will work for your need.But you can try this.

While instantiating you can define the parent. Instantiate(Object_B, transform.position, transform.rotation,holder.transform); holder is my parent object.

Then obtain the info from accessing its child objects. which will be prefabs you instantiated.

1 Reply

· Add your reply
  • Sort: 
avatar image
2

Answer by Mehul-Rughani · Dec 05, 2017 at 11:55 AM

Hello @GeorgeLuther,

Please check this solution

In Script_A script take one list of Script_B Component and when you instantiate B object add it to the list and access it wherever you want.

 public class Script_A : MonoBehaviour
 {
 
     public GameObject prefab;
     public List<Script_B> generatedObjects = new List<Script_B> ();
     // Use this for initialization
     void Start ()
     {
         
     }
     
     // Update is called once per frame
     void Update ()
     {
         if (Input.GetKeyDown (KeyCode.Space)) {
             for (int i = 0; i < 3; i++) {
                 GameObject go = Instantiate (prefab, transform.position, transform.rotation)as GameObject;
                 Script_B secondObject = go.GetComponent<Script_B> ();
                 generatedObjects.Add (secondObject);
             }
         }
 
         if (Input.GetKeyDown (KeyCode.A)) {
             for (int i = 0; i < generatedObjects.Count; i++) {
                 Debug.Log (generatedObjects [i].randomNumber);
             }
         }
     }
 }
 

and

 public class Script_B : MonoBehaviour
 {
 
     public int randomNumber;
 
     // Use this for initialization
     void Start ()
     {
         randomNumber = Random.Range (1, 10);
     }
     
     // Update is called once per frame
     void Update ()
     {
         
     }
 }


Hope you are looking for this

Regards

Comment
Add comment · 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

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

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

91 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Instantiate a COMPLETELY Unique Instance of an Object 1 Answer

How to get the instance of a ScriptableObject 3 Answers

How do I get the global position 2 Answers

How to instantiate a prefab each time I click the UI button? (getting it only once when I click the button) 4 Answers

How to manipulate a variable of a prefab script (instantiated) while the game is runnning . 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