Hello guys,
I am quite newcomer about C# scripting and I am working on an application. It is a simple “click and let some event happen” type app. I have a button and that button has animations on it. And also it includes a script that tells the button what to do. I want to deactivate that script after using the button and I want to use another button and another script in the same scene. I tried “Destroy(this);” but this time all the scene is not working. I want to start another script after destroying the current and destroyed script. I would be appreciated for helps. My sample code is below. As you see I need to start another script after destroying this one.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayAnimTrigger : MonoBehaviour
{
public GameObject MainSwitch;
public GameObject UpperLamps;
private Animator SwitchUp;
private Animator SwitchDown;
public GameObject UpperDigits;
public GameObject UpperLights;
public GameObject UpperEightDigits;
public GameObject BottomEightDigits;
public GameObject BottomLights;
public GameObject CkptEndLights;
public GameObject BottomDigits;
bool clickedOnce;
bool switchDownStarted;
void Start()
{
SwitchUp = MainSwitch.GetComponent<Animator>();
switchDownStarted = false;
}
void Update()
{
if (Input.GetMouseButtonDown(0))
{
if (!clickedOnce)
{
SwitchUp.GetComponent<Animation>().Play("SwitchUp");
SwitchUp.GetComponent<AudioSource>().Play(0);
UpperLamps.GetComponent<Animation>().Play("UpperLampsOn");
UpperDigits.SetActive(true);
UpperLights.SetActive(true);
clickedOnce = true;
}
else
{
switchDownStarted = true;
SwitchUp.GetComponent<Animation>().Play("SwitchDown");
UpperEightDigits.SetActive(true);
UpperDigits.SetActive(false);
BottomEightDigits.SetActive(true);
CkptEndLights.SetActive(true);
BottomLights.GetComponent<Animation>().Play("BottomLamps");
UpperLamps.GetComponent<Animation>().Play("UpperLampsOn");
}
}
if (switchDownStarted)
{
if (!SwitchUp.GetComponent<Animation>().IsPlaying("SwitchDown"))
{
switchDownStarted = false;
UpperDigits.SetActive(true);
UpperEightDigits.SetActive(false);
BottomEightDigits.SetActive(false);
CkptEndLights.SetActive(false);
BottomDigits.SetActive(true);
Destroy(this);
}
}
}
}
`