- Home /
EDITED rend.material change too hard to handle for iOS
EDIT: The game doesn't crash when I disable this script for all cars, which means that this script is the problem.
Hello!
I am having trouble with changing my car's color. Could this be too hard for iOS to handle?
void Awake ()
{
if (PlayerPrefs.GetInt ("Selected Car") == 1) {
if (PlayerPrefs.GetInt ("Selected Colour 11") == 1) {
rend.material = Car01materials [0];
}
if (PlayerPrefs.GetInt ("Selected Colour 11") == 2) {
rend.material = Car01materials [1];
}
if (PlayerPrefs.GetInt ("Selected Colour 11") == 3) {
rend.material = Car01materials [2];
}
if (PlayerPrefs.GetInt ("Selected Colour 11") == 4) {
rend.material = Car01materials [3];
}
if (PlayerPrefs.GetInt ("Selected Colour 11") == 5) {
rend.material = Car01materials [4];
}
if (PlayerPrefs.GetInt ("Selected Colour 11") == 6) {
rend.material = Car01materials [5];
}
}
}
The game crashes when I load level 1 on my iPhone. Works fine in Unity.
What does your log say?
Also you can optimize above code with a switch().
void Awake ()
{
if (PlayerPrefs.GetInt ("Selected Car") == 1) {
switch(PlayerPrefs.GetInt ("Selected Colour 11"))
{
case 1:
rend.material = Car01materials [0];
break;
case 2:
rend.material = Car01materials [1];
break;
case 3:
rend.material = Car01materials [2];
break;
case 4:
rend.material = Car01materials [3];
break;
case 5:
rend.material = Car01materials [4];
break;
case 6:
rend.material = Car01materials [6];
break;
default:
// Load first material as default
rend.material = Car01materials [0];
break;
}
}
}
Also if you are following the pattern found in your code above. You can do it in one line also as:
void Awake ()
{
if (PlayerPrefs.GetInt ("Selected Car") == 1) {
rend.material = Car01materials [PlayerPrefs.GetInt ("Selected Colour 11") - 1];
}
}
But the second method is not much of stable one.
Thanks for your comment! Previously I tried to make the colour thing work with .SetActive.
I added six different car bodies to six different cars and the game crashed when I loaded level 1. Now I only have one car body on each car, but something makes the game crash.
The game doesn't crash if I only have one colour per car.
How does the the switch case change my script performance wise?
Don't know about performance, but I think it's not even worth thinking about it. For your problem. I don't think the crash happens because of this script. I'd also not assign a whole new material but change it's color property. With these, you could have color pickers in the inspector, would make things more easy (if it's just a tint you're using of course).
Have you tried compiling both 32 and 64 bits? What does XCode console say at that point?
How does the the switch case change my script performance wise?
switch vs if else performance c#
But the case here is that it might not be the if else ladder that might be the cause. And that is why I asked you for the log. I think you should post your log here for perusal.
@Harshad$$anonymous$$ Unloading 87 unused Assets to reduce memory usage. Loaded Objects now: 986. Total: 30.087999 ms (FindLiveObjects: 0.383208 ms CreateObject$$anonymous$$apping: 0.078083 ms $$anonymous$$arkObjects: 2.247333 ms DeleteObjects: 26.832251 ms)
2015-04-14 16:04:41.863 xxxxxxx[1832:491408] Communications error: { count = 1, contents = "XPCErrorDescription" => { length = 22, contents = "Connection interrupted" } }>
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
C# and JavaScript Unstable? 1 Answer
[SOLVED] How to make a mesh with mobile/bumped diffuse shader transparent? 1 Answer
WebCamTexture IOS crashing? 1 Answer
Distribute terrain in zones 3 Answers