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 Oorlfe · Jan 18, 2017 at 11:18 PM · other scriptother object variables

How to change a variable on the script of an instance that is one of many

Hi,

I have a prefab that is repeated many times to make many instances. It has a script on it with a variable. I can access the instances individually and I know how to call to a function in another script to change the variable in it.

My understanding is that by using the command (after the script is found and thisScript has it assigned to it):

 thisScript.thisScriptFunction (variable);

I will end up triggering thisScriptFunction and changing the variable on every instance that has the script on it. If I want to only trigger the change on one instance of the many how do I specify in the command?

for example:

 thisInstance.faceScript.FaceChangeCount (groupTrackerH);

does not work.

Thank you for any help!

Comment
Add comment · Show 3
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 Tobychappell · Jan 18, 2017 at 11:44 PM 0
Share

Would you be able to post the code?

avatar image HenryStrattonFW · Jan 19, 2017 at 12:10 AM 0
Share

I converted your response into a comment as you should only post as an answer if you are offering an answer to the question. However for some reason whilst the hide/show button suggests the comment is there It isn't rendering for me, so my apologies if I've done that wrong and it's broken the comment. But I've updated my answer to include some code that hopefully will help explain what you are trying to do.

avatar image EDevJogos · Jan 19, 2017 at 01:24 AM 0
Share

Could you give more details of the game itself? it would help a lot to have some understanding of what you actually want and think of a solution, your question is too vague.

1 Reply

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

Answer by HenryStrattonFW · Jan 18, 2017 at 11:46 PM

Your statement that calling the function on the found item effects all instances is not correct (unless I misunderstand you). This would only be true if the function you are calling is static to the type, or if the variable that function is modifying is static to the type.

If you have run your find method and have a reference to a single instance of an object, then calling methods on it (unless static) should only effect that single instance (unless as previously mentioned that function modifies static variables on the type).

It would be easier to get a clear idea of what your issue is / what you are trying if you could provide some code to refer to.

EDIT: Ok so I've written two monobehaviours that should demonstrate the idea of how to set a variable on an instance of a script individually.

 public class OneOfMany : MonoBehaviour
 {    
     private string message  = "none";
 
 
     public void SetMessage(string newMessage)
     {
         message = newMessage;
     }
 }
 
 
 
 
 public class Test : MonoBehaviour
 {
     
     public void SetMessages ()
     {
         OneOfMany[] many = GameObject.FindObjectsOfType<OneOfMany>();
         
         for(int i=0;i<many.Length;i++)
         {
             many[i].SetMessage("I was number: "+i.ToString());
         }
     }
 }


You can apply the OneOfMany script to multiple objects in your scene, then if you add Test to one object and then ran the SetMessages method, it finds ALL of the OneOfMany scripts in the scene, and sets the message of each one which will then be unique.

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 Oorlfe · Jan 19, 2017 at 02:35 AM 0
Share

Great, thank you!

If I have the following code to gather up all the magenta faces and their corresponding scripts:

     public class Test : $$anonymous$$onoBehaviour
     {
          private GameObject[] all$$anonymous$$aj;
          private GameObject[] all$$anonymous$$ajScripts;
          private FaceScript[] faceScriptArray;
     
          void Start ()
          {
               faceScriptArray = GameObject.FindObjectsOfType<FaceScript>();
          }
     
          void Check$$anonymous$$ajenta ()
          {
               int m = 0;
     
               for (int f = 0; f < faces.Length; f++)
             {
                 if (faces[f].activeSelf)
                 {
                     if (faces[f].tag == "$$anonymous$$aj Face")
                     {
                         all$$anonymous$$aj[m] = faces[f];
                         all$$anonymous$$ajScripts[m] = faceScriptArray[f];
                         m++;
                     }
                 }
             }
          }
     }

Question 1 is that I get an error saying, "Cannot implicitly convert type 'FaceScript' to 'UnityEngine.GameObject'." What should it be?

Question 2 is will the instance and its script be on the same number in their arrays so that I can say, if its face number 4 then set the variable of script number 4 to whatever?

Thank you again!

avatar image aditya Oorlfe · Jan 19, 2017 at 05:15 AM 2
Share

"Cannot implicitly convert type 'FaceScript' to 'UnityEngine.GameObject'." because faceScriptArray is of object type FaceScript and all$$anonymous$$ajScripts is of type GameObject ...

 all$$anonymous$$ajScripts[m] = faceScriptsArray[f].gameObject;

 
avatar image Oorlfe aditya · Feb 16, 2017 at 12:19 AM 0
Share

great 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

62 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

Related Questions

Getter function not returning correct value from another script (C# scripting) 1 Answer

Raycast to trigger audio from other script on hit? 2 Answers

How to access a trigger objects variables: 2 Answers

Missing reference to object 1 Answer

Change Weapon by its Script int 0 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