For my school project, I’ve designed a solar system simulator and one of my last success criteria seems very simple but I just can’t get it to work at all.
What I want is a slider which dictates how long a planet will last for before being destroyed as I’ve stated this as being one of my success criteria.
I already have an add planet button which works fine but I want the user to be able to adjust how long it’ll last.
I’ve already created the slider in the inspector, I just want the method to go along with it
Here’s my current code:
using System.Collections;
using UnityEngine;
public class AddPluto : MonoBehaviour {
public GameObject clone;
public GameObject Pluto;
public float duration = 50f;
public void PlanetDuration(float duration)
{
}
public void AddObject()
{
//Creates clone of Pluto
clone = Instantiate(Pluto, new Vector3 (2F, 0, 0), Quaternion.identity);
Destroy (clone, duration);
}
}
I actually figured it out. It’s just that I had to use UnityEngine.UI and use the slider reference. It seems like such a simple thing to implement, yet I’ve spent the last 3 hours trying to figure it out.
This is now the amended code:
using System.Collections;
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class AddPluto : MonoBehaviour {
public GameObject clone;
public GameObject Pluto;
public Slider PlanetDuration;
public void AddObject()
{
//Creates clone of Pluto
clone = Instantiate(Pluto, new Vector3 (2F, 0, 0), Quaternion.identity);
// Destroys clone after 60 seconds
Destroy (clone, PlanetDuration.value);
}
public void duration(float newPlanetDuration){
PlanetDuration.value = newPlanetDuration;
}
}