- Home /
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) ;
}
}
Format your code properly with the 101010 button and please post text rather than a screener of code :)
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;
}
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.
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 :)
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 :(
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;
}
Your answer
Follow this Question
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