- Home /
Changing the material at runtime for multiple objects in a single call
Hey guys!
I am newbie to unity please help me out form this, i am trying to make a unlimited 2d runner game and i have 5 environments to swap as per the user selection, on the play button event i am calling the "SetTheBackground()" method out of 9 bgs only one got effected. (may be the problem is in single frame it has been called). if i kept the folowwing code in update method it's working but fps issue.
we should not call in every frame. please help me out.
using UnityEngine; using System.Collections;
public class BackgroundManager : MonoBehaviour { public static BackgroundManager instance;
GameObject mgameObject; public Material[] bgmaterial;
void Awake() { mgameObject = this.gameObject; instance = this; }
public void SetTheBackground() { if (GameController.instance != null) { switch (GameController.instance._ThemeType) { case eTheme.Theme1: mgameObject.renderer.material= bgmaterial [0]; break; case eTheme.Theme2: mgameObject.renderer.material = bgmaterial [1]; break; case eTheme.Theme3: mgameObject.renderer.material = bgmaterial [2]; break; case eTheme.Theme4: mgameObject.renderer.material = bgmaterial [3]; break; case eTheme.Theme5: mgameObject.renderer.material = bgmaterial [4]; break; default: break;
}
}
else {
}
} Thanks in advance!