- Home /
The best methods for changing objects within a scene?
Hello, I've been looking into how to replace a GameObject with another GameObject. Allow me to explain: I have a SpringTree GameObject and a WinterTree GameObject. I already have a script that changes the season by using a String Variable currentSeason.
Basically, I want my objects to change their appearance based on what season it is. If it is Spring, use spring objects, etc. I have tried several different methods, such as using the SetActive method, but it has not worked. Is there any other way to accomplish what I am looking for?
Here was my last attempt that was based off another post I found. The script references another script that holds the value of the currentSeason.
Any help is greatly appreciated!
public GameObject sceneController;
private SeasonController seasonControllerScript;
public GameObject springObject;
public GameObject winterObject;
// Use this for initialization
void Start () {
seasonControllerScript = sceneController.GetComponent<SeasonController>();
if (seasonControllerScript.currentSeason == "Spring")
{
springObject.SetActive(true);
}
else if (seasonControllerScript.currentSeason == "Winter")
{
winterObject.SetActive(true);
}
}
// Update is called once per frame
void Update () {
if (seasonControllerScript.currentSeason == "Spring")
{
springObject.SetActive(true);
winterObject.SetActive(false);
}
else if (seasonControllerScript.currentSeason == "Winter")
{
winterObject.SetActive(true);
springObject.SetActive(false);
}
}
I assume you do not need to replace trees with other trees for simulating seasons, y'd better create smooth material transition from spring to winter for bark and leaves.
Are the spring and winter objects both in the scene or are you dragging in their prefabs from your project folder? If they're only prefabs and not in your hierarchy then SetActive will not do anything.
Answer by ComicGirl · Nov 29, 2017 at 02:27 AM
Thanks for all the replies! IPVDI was correct. Instead of using two different objects, I instead created the two different materials that represent the seasons, then created the script below that changes the materials based on the season. Thanks again for all the help, I appreciate the help with the direction! Script tested and works!
public GameObject sceneController;
private SeasonController seasonControllerScript;
public Material springMaterial;
public Material winterMaterial;
// Use this for initialization
void Start () {
seasonControllerScript = sceneController.GetComponent<SeasonController>();
if (seasonControllerScript.currentSeason == "Spring")
{
GetComponent<Renderer>().material = springMaterial;
}
else if (seasonControllerScript.currentSeason == "Winter")
{
GetComponent<Renderer>().material = winterMaterial;
}
}
// Update is called once per frame
void Update () {
if (seasonControllerScript.currentSeason == "Spring")
{
GetComponent<Renderer>().material = springMaterial;
}
else if (seasonControllerScript.currentSeason == "Winter")
{
GetComponent<Renderer>().material = winterMaterial;
}
}
Your answer
Follow this Question
Related Questions
game object set active on trigger enter? 3 Answers
Need help with C# code. both gameObject and col.gameObject are getting destroyed. here is the code. 2 Answers
How to swap gameObjects on the press of a button? 1 Answer
activate GameObject dont work 1 Answer
Setting Without Altering Object Scale and Collider Scale 0 Answers