- Home /
Change Material in other Scene by Scripting
I have created a GAME. I created 2 scenes. A MENU and the actual GAME. In the MENU I can change colors with the understanding script.
var StockCarMats : Material[];
var StockWheelMats : Material[];
var Car : GameObject;
var FLWheel : GameObject;
var FRWheel : GameObject;
var BLWheel : GameObject;
var BRWheel : GameObject;
var Oranje : Material;
var Blauw : Material;
var Zwart : Material;
var Geel : Material;
var Wit : Material;
var Rood : Material;
var CarTurn : Transform;
var Livery = 1;
function Livery2()
{
var CarMats = Car.renderer.materials;
var FLWheelMats = FLWheel.renderer.materials;
var FRWheelMats = FRWheel.renderer.materials;
var BLWheelMats = BLWheel.renderer.materials;
var BRWheelMats = BRWheel.renderer.materials;
CarMats[0] = Oranje;
CarMats[1] = StockCarMats[1];
CarMats[2] = StockCarMats[2];
CarMats[3] = StockCarMats[3];
CarMats[4] = StockCarMats[4];
CarMats[5] = StockCarMats[5];
CarMats[6] = Zwart;
CarMats[7] = StockCarMats[7];
CarMats[8] = StockCarMats[8];
CarMats[9] = StockCarMats[9];
CarMats[10] = StockCarMats[10];
FLWheelMats[0] = Zwart;
FLWheelMats[1] = Wit;
FLWheelMats[2] = StockWheelMats[2];
FRWheelMats[0] = Zwart;
FRWheelMats[1] = Wit;
FRWheelMats[2] = StockWheelMats[2];
BLWheelMats[0] = Zwart;
BLWheelMats[1] = Wit;
BLWheelMats[2] = StockWheelMats[2];
BRWheelMats[0] = StockWheelMats[2];
BRWheelMats[1] = Zwart;
BRWheelMats[2] = Wit;
Car.renderer.materials = CarMats;
FLWheel.renderer.materials = FLWheelMats;
FRWheel.renderer.materials = FRWheelMats;
BLWheel.renderer.materials = BLWheelMats;
BRWheel.renderer.materials = BRWheelMats;
}
function OnGUI()
{
if(GUI.Button(Rect(10,90,70,30),"Livery 2"))
{
Livery2();
Livery = 2;
}
}
Of course when I start the GAME Scene this changed color is lost. I tried to fix this with another script in the GAME Scene.
if(GUI_Script.Livery == 2) {
GUI_Script.Livery2();
}
This brings up some errors. Makes sense since I pretty much have to change all the variables to static variables. But when I do this to all the needed variables, Unity loses the links to GameObjects and Materials in the Inspector. And then it generates errors becaus of the lost links. And I can't restore the links in the Inspector because they don't show up until I revert all the static var's back to normal variables and delete the second script.
Is this a bug? Is there a maximum to static var's to define? Can't a static var be a GameObject, Material or array? Or is there another solution?
Your answer
Follow this Question
Related Questions
I've done something wrong in the tiling, any ideas? 1 Answer
How to change the material of a sub-instance of one prefab ? 2 Answers
Scene Change Event 1 Answer
Health script, save health in between scenes. PlayerPrefs. 2 Answers
How to create a GUI button to change a character's texture in real-time? 2 Answers