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 nitesh · Dec 16, 2014 at 11:04 AM · guimaterialcolour

script not working on multiple objects

created a simple cube and applied a material ad some how i managed to write a script to change the color of that material when a GUI button is pressed and its working fine.. Then created a capsule in the same scene then applied same script on it its working for Capsule but not for my previous game object then applied on a new cube then it stops working for last two game objects..

done by defining a variable Texture_Index, when i manually change the texture index value for each object(in run time) its working,but not working when GUI button is pressed..

here my uJS script :

 #pragma strict
 
 var TextureIndex : int;
 
 function OnGUI()
 {
 
  GUI.Box (Rect (10,400,100,100), "Box Texture");
 
  //1st button
 
  if (GUI.Button (Rect (20,425,80,20), "1")) {
        
     TextureIndex = 0;
     }
 
 
  if (GUI.Button (Rect (20,450,80,20), "2")) {
    
     TextureIndex = 1;
     }
 
  }
 
  function Update()
  {
 
  if(TextureIndex == 0)
  {
 
  //color code = rgb value devided by 255 :)73 32 3 255
 
  renderer.materials[0].color = Color.blue;
  }
 
  else if (TextureIndex == 1)
  {
 
  renderer.materials[0].color = new Color (73/255F,32/255F,3/255F,1F) ;
  }
 }
script.jpg (46.2 kB)
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 meat5000 ♦ · Dec 16, 2014 at 12:15 PM 0
Share

Format your code properly with the 101010 button and please post text rather than a screener of code :)

avatar image nitesh · Dec 16, 2014 at 12:27 PM 0
Share

@meat5000 text is also there..

avatar image nitesh · Dec 17, 2014 at 11:27 AM 0
Share

using this code to solve my problem but showing error (24,48): BCE0044: unexpected char: 0x201C.

what does it means?? confused :/

pragma strict

var TextureIndex : int;

var coloredItem;

var object : GameObject[];

function OnGUI()

{

GUI.Box (Rect (10,400,100,100), "Box Texture");

if(GUI.Button (Rect (20,425,80,20), "red"))

{

print("zooo");TextureIndex = 0;

}

if (GUI.Button (Rect (20,450,80,20), "blue"))

{

print("2ooo");TextureIndex = 1;

}

}

function Update()

{

if(TextureIndex == 0)

{

coloredItem =GameObject.FindGameObjectsWithTag(“circle”); }

else if (TextureIndex == 1)

{

coloredItem =GameObject.FindGameObjectsWithTag(“sphere”);

}

if(coloredItem!=null)

{

for (var respawn:GameObject in coloredItem)respawn.renderer.material.color=Color.blue;

}

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by SkaredCreations · Dec 16, 2014 at 11:12 AM

Besides the fact that this is not a C# script but Javascript, if you're using the same script for two objects then what you'll see is the GUI buttons for both objects displayed at the same position so you will be able to change the color of only one of them because the other's buttons are behind and by the way cannot be clicked. To use the same script you should at least be able to set the buttons position from inspector, an alternate way would be to have a Renderer object in the class called for example "target" and attach it to an empty game object so this way you could drag&drop the object that you want to change as well as assign it programmatically in your code.

Comment
Add comment · Show 4 · 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 nitesh · Dec 16, 2014 at 11:23 AM 0
Share

haah..sorry...ya its java :p my mistake..

Thanx for your reply..

i think i got ur words..let me try..thank you soo much :)

avatar image nitesh · Dec 16, 2014 at 12:13 PM 0
Share

@skaredCreations

applied my previous script to the empty game object and in script i defined a variable called "objects" type game object with Boolean value..and added all of my 3 game objects to the script in inspector view.. in my script i want to write that change the color value for all the selected game object..i dnt know how to write it in java.. coz i dont have much knowledge about languages :(

avatar image meat5000 ♦ · Dec 16, 2014 at 12:14 PM 0
Share

Time to learn?

avatar image nitesh · Dec 16, 2014 at 12:28 PM 1
Share

@meat5000 ya i think so.. finally its time to learn :/

avatar image
0

Answer by nitesh · Dec 17, 2014 at 11:34 AM

using this code to solve my problem but showing error (24,48): BCE0044: unexpected char: 0x201C.

what does it means?? confused :/ pragma strict

var TextureIndex : int;

var coloredItem;

var object : GameObject[];

function OnGUI()

{

GUI.Box (Rect (10,400,100,100), "Box Texture");

if(GUI.Button (Rect (20,425,80,20), "red"))

{

print("zooo");TextureIndex = 0;

}

if (GUI.Button (Rect (20,450,80,20), "blue"))

{

print("2ooo");TextureIndex = 1;

}

}

function Update()

{

if(TextureIndex == 0)

{

coloredItem =GameObject.FindGameObjectsWithTag(“circle”); }

else if (TextureIndex == 1)

{

coloredItem =GameObject.FindGameObjectsWithTag(“sphere”);

}

if(coloredItem!=null)

{

for (var respawn:GameObject in coloredItem)respawn.renderer.material.color=Color.blue;

}

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Changing the tint colour of materials 1 Answer

How to change the colour of my car? 1 Answer

Making Object Flash? 1 Answer

Updating custom material properties for RawImage 2 Answers

Menu material error 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