How to change game object material in game mode so that it would stay after game mode?

Hello, I was following a tutorial to make a material radial menu, to be able to change materials on game mode, although after I stop the game mode, the material switches back to the original material that was applied before game mode.

The goal is not just temporarily change the material during game mode, but switch it permanently, so that when going back to the scene editing mode, the material change made in game mode would stay after running. The unity materials are linked to the resonance audio materials, therefore, when changing the object materials manually, the sound in the game changes as well depending on the material applied, but since using the current script the materials are applied somehow temporarily - they do not have any affect on the sound. The project is about sound analyses in buildings, where we analyze different building materials and how they affect the acoustics and sound in offices using VR Oculus.

The tutorial followed:

Scripts:

Radial button:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class RadialButton : MonoBehaviour
{
[HideInInspector]
public string title;
[HideInInspector]
public Material material;
[HideInInspector]
public MaterialChanger obj;

private MeshRenderer childRenderer;
private bool buttonEnabled;

// Start is called before the first frame update
void Start()
{
childRenderer = transform.GetChild(0).gameObject.GetComponent();
childRenderer.material = material;
StartCoroutine(EnableButton());
}

private IEnumerator EnableButton()
{
yield return new WaitForSeconds(0.3f);
buttonEnabled = true;
}
public void OnPointerEnter()
{
obj.pointerOverButton = true;
if (buttonEnabled == true)
{
obj.objectToChange.material = material;
obj.objectToChange2.material = material;
obj.objectToChange3.material = material;
obj.objectToChange4.material = material;
obj.objectToChange5.material = material;
}
}

public void OnPointerExit()
{
obj.pointerOverButton = false;
if (buttonEnabled == true)
{
StartCoroutine(obj.DestroyMenu());
}
}

public void OnPointerDown()
{
obj.objectToChange.material = material;
}
}

Material changer:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class MaterialChanger : MonoBehaviour
{
public RadialMenu menuPrefab;
public Renderer objectToChange;
public Renderer objectToChange2;
public Renderer objectToChange3;
public Renderer objectToChange4;
public Renderer objectToChange5;

[HideInInspector]
public bool pointerOverButton, pointerOverMenu;

[System.Serializable]
public class MaterialInfo
{
//SET FIELDS UNDER MATERIAL CHANGER
public string title;
public string Resonance_Material;
public Material material;
}

public MaterialInfo[ ] materials;

private RadialMenu currentMenu;
private Transform childCollider;

// Start is called before the first frame update
void Start()
{
childCollider = transform.GetChild(0);
}

public void OnPointerEnter()
{
pointerOverMenu = true;
// SPAWN A MENU

if (currentMenu == null)
{
currentMenu = Instantiate(menuPrefab) as RadialMenu;
currentMenu.transform.position = transform.position;
currentMenu.transform.rotation = transform.rotation;
childCollider.localScale = new Vector3(0.85f, 2.3f, 2.3f);

//SPAWN BUTTONS
currentMenu.SpawnButtons(this);

}
}

public void OnPointerExit()
{
pointerOverMenu = false;
StartCoroutine(DestroyMenu());

}

public IEnumerator DestroyMenu()
{
yield return new WaitForEndOfFrame();
if (pointerOverButton == false && pointerOverMenu == false)
{
Destroy(currentMenu.gameObject);
childCollider.localScale = new Vector3(0.25f, 0.6f, 0.6f);
iTween.ScaleTo(currentMenu.transform.GetChild(0).gameObject, iTween.Hash(“scale”, Vector3.zero, “time”, 1f, “oncomplete”, “DestroyMenu”, “oncompletetartget”, gameObject));
}
}

}

You can’t have changes saved automagically in runtime. After being build, the assets are readonly. You must manually save write your changes to some external file or PlayerPrefs and after restart read that data and restore changes.

1 Like