- Home /
Creating Material from String (Unity 5.6)
Hi all,
I am trying to create a material using a string which is now obsolete. I have the following code currently but I receive an error (following code) and I am unsure how to fix it.
private string skyMat;
skyMat = PlayerPrefs.GetString("skybox");
RenderSettings.skybox = new Material(Application.dataPath + "/Resources/Skyboxes/" + skyMat + ".mat");
Error:
Trying to create a material from string - this is no longer supported.
UnityEngine.Material:.ctor(String)
What I want to do is to find the material which is a string saved to PlayerPrefs and set the skybox to it. Also I'm not sure how to set skyboxes up nowadays. Is it still RenderSettings.skybox
?
Thanks in advance,
Cordel
Answer by Trevdevs · Jun 28, 2017 at 07:49 AM
Sorry but there is no way to do this anymore, and quite honestly thankfully it is gone.
Simplest thing to do now would just be
public Material skyboxMaterial; //Assign in inspector
void Start()
{
RenderSettings.skybox = skyboxMaterial;
}
That's a bummer, because all of my levels have randomly generated skyboxes and so when the level is completed, it would make sense to have the same skybox as the level, which is why I saved the name of it to the PlayerPrefs to reload it in the LC scene.
What you could do is create an array of $$anonymous$$aterials and do a Random.Range on an int to select one of the skyboxes at random.
public $$anonymous$$aterial[] skybox$$anonymous$$aterials; //Still all assigned in the inspector
void Start()
{
int randomInt = Random.Range(0, skybox$$anonymous$$aterials.Count + 1 );
RenderSettings.skybox = skybox$$anonymous$$aterials[randomInt];
}
Thanks mate. Just re-worked it to have an object with a separate script with a DontDestroyOnLoad to keep an array of skyboxes and just allocate the number to it each time.