Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 Project_Yauler · Aug 18, 2015 at 08:38 PM · prefabinstance

Changing the Variables of An Instanced Script

I've got a prefab that has an attached VariableScript with these variables:

 public int value = 0;
 public bool isVisible = false;
 public bool isMine = false;
 public bool isFlag = false; 

I have another script that accesses these variables and changes them:

 variable_script = mineFieldArray[x, y, z].GetComponent<VariableScript>();
 variable_script.isMine = true;

'mineFieldArray' is a 3D array that holds each instanced predfab, each with its own VariableScript.

However, when I run the script, I've found that it changes the value of 'isMine' to 'true' for all of the instanced scripts. Am I doing something wrong? It appears to be changing the value in the original script (i.e. for the prefab) itself rather than a particular instance.

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 Jessespike · Aug 18, 2015 at 08:42 PM 0
Share

show the code that instantiates

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by FortisVenaliter · Aug 18, 2015 at 08:44 PM

How do you fill the array? If you are generating the instances at loadtime or runtime, then you need to make sure you are assigning the array values from the instantiated object, not the prefab you are instantiating them from.

Comment
Add comment · Show 3 · 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 Project_Yauler · Aug 19, 2015 at 03:16 PM 0
Share

This is the code that creates the instances and fills the array (3x3x3):

     for (x = 0; x < 3; x++) {
         for (y = 0; y < 3; y++) {
             for (z = 0; z < 3; z++) {
                 (Instantiate ($$anonymous$$e, new Vector3 (myX, myY, myZ), Quaternion.identity) as GameObject).transform.parent = gameObject.transform;
                 $$anonymous$$eFieldArray[x, y, z] = $$anonymous$$e;
                 myZ += 2;
             }
             myY += 2;
             myZ = -2;
         }
         myX += 2;
         myY = -2;
     }

I suspect that it's something to do with the 'Instantiate' line, as I took a look at the prefab after running the code and found I had changed the prefab's variables aswell.

avatar image FortisVenaliter · Aug 19, 2015 at 07:47 PM 0
Share

Yeah, that's the problem. "$$anonymous$$e" is presumably set to the prefab, correct? Instantiate returns a value that is the new instance. So you want something like this:

 $$anonymous$$eFieldArray[x,y,z] = Instantiate(...
avatar image Project_Yauler · Aug 20, 2015 at 02:59 PM 0
Share

Ah right, that makes sense! That works now. Thanks for the help!

avatar image
0

Answer by DizzyWascal · Aug 19, 2015 at 02:31 PM

You can try instantiating your gameObjects into a node and use the GetComponentsInChildren to obtain them into arrays.

 using UnityEngine;
 using UnityEngine.UI;
 
 public class Example: MonoBehaviour 
 {
     //Make sure this parent gameobject is in the scene because we're instantiating the child 
     //objects and then parenting them to it. OR you may already have a location for these objects.
     public GameObject parent;
     public GameObject childObjects;
     
     //The script array you want to get
     private ExampleScript[] exampleScript; //Make sure this is attacked to the child objects
 
     void Start()
     {
         //Just a scenario when these objects should be instantiated
         //Like instantiate 20 times I guess
         for(int i = 0; i < 20; i++)
         {
             //Instantiate your objects
             Transform childObj = Instantiate(childObjects, parent.position, parent.rotation) as Transform;
             //Parent each spawned child to a parent object within each loop
             childObj.SetParent(parent, true);
         }
         
         //After your objects have been spawned, grab the script you want from them inside the parent node
         exampleScript = parent.GetComponentsInChildren<ExampleScript>();
         
         //You now have an array of the script.
         //You can now change the variables
         exampleScript[0].exampleVariable = true;
     }
 }
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.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

How do I re-link an object that has unintentionally become detached from prefab? 0 Answers

Script uses transform.position of Prefab instead of Instance 1 Answer

How (and where) does Unity save the modified parameters of a prefab's instance? 1 Answer

Two Instances of same Prefab dealing damage to each other. 0 Answers

How do I count how many instances of a prefab there are? 3 Answers


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