• 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

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

93 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 avatar image avatar image

Related Questions

Instantiate a COMPLETELY Unique Instance of an Object 1 Answer

Instantiated object is destroyed in scene, but still logs as existing in console. 1 Answer

Scriptable Object storing Instantiated Objects Show Mismatch Types 1 Answer

UNITY 5 - SPACE SHOOTER - INSTANTIATE NOT SPAWNING 3 Answers

I need help with the location of instatiated objects on the scene and correct interaction with this 2 Answers

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