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 Jish · Apr 10, 2014 at 06:03 AM · getcomponentloop

FindGameObjectsWithTag Changing Variable on Another Script Not Fully Working

As the title says i'm trying to change a colour variable on multiple objects through a gui button press. At the moment when i click the button it changes most of the objects but for seemingly no reason doesn't effect some. They all have the exact same tags and components yet they remain unaltered.

     f

 unction OnGUI(){
             Canada = GameObject.FindGameObjectsWithTag("Canada");
             
             if(GUI.Button(Rect(10,10,100,30),"Button Label")){
                 if(showMenu == false)
                     showMenu = true;
                 else
                     showMenu = false;
             }
         
             if(showMenu == true){
                 if (GUI.Button(Rect(10, 50, 100, 30 ), "One")){
         
                     for (var i : int = 0; i < Canada.length; i++){
                         Canada[i].GetComponent(colorChange).colourEnd = new Color(1,1,1);
         
         }
 
 

My first thought was a there was a problem with the loop so i changed 'Canada.length' to a high number thinking it might change everything, but i had no luck with this.

Any help is much appreciated, thank you in advance.

EDIT I figured it might help to post the code be used on the objects i'm trying to effect.

 var colourStart: Color = Color.red;
 var colourEnd: Color = Color.red;;
 
  
 function Start() {
 
 }
  
 function Update () {
 
     renderer.material.color = Color.Lerp (colourStart, colourEnd, Time.deltaTime * 3);
     colourStart = rrenderer.material.color;
 
 
     
 }
Comment
Add comment · Show 2
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 AR_Rizvi · Apr 10, 2014 at 06:49 AM 0
Share

I m not sure but try putting your loop like this

for (var i : int = 1 ; i

avatar image Jish · Apr 10, 2014 at 01:27 PM 0
Share

This didn't change anything unfortunately.

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by haim96 · Apr 10, 2014 at 06:53 AM

does the number of object with tag "canada" changed during running? if not i would suggest to move Canada = GameObject.FindGameObjectsWithTag("Canada"); to Start() so it will happen only once when the script start and not every time that gui update.

also if canada is an array(and it should be), make sure you declared enough for all the object in scene.

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 Jish · Apr 10, 2014 at 01:17 PM 0
Share
 var Canada : GameObject[];

Should mean there is enough i think.

avatar image
0

Answer by maddFrogg · Apr 10, 2014 at 06:59 AM

Hi, I'd remove this line from where it is written

 Canada = GameObject.FindGameObjectsWithTag("Canada");

as it executes in every frame with no use at all, and I'd place it here:

 [...]
 if(showMenu == true){
     if (GUI.Button(Rect(10, 50, 100, 30 ), "One")){
         Canada = GameObject.FindGameObjectsWithTag("Canada");
         for (var i : int = 0; i < Canada.length; i++){
              Canada[i].GetComponent<colorChange>().colourEnd = new Color(1,1,1);
         }
     }
 }

Note I also changed the GetComponent statement, I usually use it this way...

Comment
Add comment · Show 2 · 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 Jish · Apr 10, 2014 at 01:09 PM 0
Share

This didn't work, when i tried to use the GetComponent like you do it would just give me errors.

avatar image maddFrogg · Apr 11, 2014 at 06:42 AM 0
Share

I'm stuck at this

 Canada[i].GetComponent(colorChange).colourEnd = new Color(1,1,1);

What is colorChange is it a method?

You may try this

 Canada[i].GetComponent<_NameOfYourClass_>().colourEnd = new Color(1,1,1);

Where NameOfYourClass is the name of the class you posted, containing the colourEnd variable and stuff

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

24 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

Related Questions

Using arrays and for loop with GetComponent error 1 Answer

How do i get script variables from a prefab inside a list? 1 Answer

Loop through Children Unable to Access Component Script 1 Answer

Guarantee loop order of child objects. 1 Answer

I dont know why im getting a null reference exception 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