Anyone have any other ideas? I’ve tried these and sadly they don’t seem to work.
I’ve been having some trouble with this for a while. I have a test game where I have a player going around picking up spheres. I would like to have the spheres reappear in the same place again after 3 seconds. I’m currently using SetActive(false)on a player script so the sphere can be picked up and counted. In a pickup script, I have a coroutine set up to set the sphere back to active after 3 seconds. Problem is my spheres are never set back to active after 3 seconds. I read on another post (GameObject SetActive not reactivating - Unity Answers) you need to set up a parent game object with child game objects for the set active to pull from but I have no idea where to start on setting that up in my code or if it will work for my situation.
Any suggestions? Here’s my code:
Player script:
public class ThirdPersonController1 : MonoBehaviour {
//Variables for movement
public float movementSpeed = 5.0f;
float verticalVelocity = 0;
public float jumpSpeed = 20;
CharacterController character;
//Variables for Pickup and Counter
public GUIText countText; //shows how many spheres have been picked up
public GUIText winText; //shows message when you win the game
private int count; //counting the spheres as they are picked up
public bool guiIsOn = false;
//bool isPickedUp = false;
void Awake()
{
Time.timeScale = 1;
}
// Use this for initialization
void Start ()
{
count = 0; //starts count at 0
SetCountText(); //called method to add spheres as they are picked up
winText.text = ""; //empty at game start
}
// Update is called once per frame
void Update ()
{
//Get the Character Controller//
CharacterController character = GetComponent<CharacterController>();
//Character Movement//
float FBSpeed = Input.GetAxis("Vertical") * movementSpeed; //forward and back movement
float LRSpeed = Input.GetAxis("Horizontal") * movementSpeed; //left and right movement
verticalVelocity += Physics.gravity.y * Time.deltaTime; //jump velocity aka how high you jump
Vector3 speed = new Vector3(LRSpeed, verticalVelocity, FBSpeed); //get the Vector for the speed
speed = transform.rotation * speed; //storing the speed
character.Move(speed * Time.deltaTime); //get the character movement speed
//Check to see if the Character is on the ground and Jump is pressed
if (Input.GetButtonDown("Jump") && (character.isGrounded))
{
verticalVelocity = jumpSpeed;
}
}
//when the player hits the game object labeled "Pickup"
//the game object will deactivate and disappear
void OnTriggerEnter(Collider other)
{
if(other.gameObject.tag =="Pickup")
{
other.gameObject.SetActive(false);
count = count + 1; //increases number of spheres picked up
SetCountText(); //called method to add spheres as they are picked up
// isPickedUp = true;
// Debug.Log ("Picked up!");
// StartCoroutine(RespawnItem());
}
}
//Respawn the pickup
// IEnumerator RespawnItem()
// {
// if(isPickedUp)
// {
// int respawnTime = 3;
// yield return new WaitForSeconds(respawnTime);
// gameObject.SetActive(true);
// Debug.Log ("Spawned!");
// }
//
// isPickedUp = false;
// }
void SetCountText() //shows the number of picked up spheres in the GUI
{
countText.text = "Count: " + count.ToString();
//if all 7 spheres are picked up show win text
if(count >= 7)
{
winText.text = "You Win!";
Debug.Log ("hey!");
Time.timeScale = 0;
guiIsOn = true;
OnGUI();
}
}
void OnGUI()
{
if (guiIsOn)
{
// Make the first button. If it is pressed, Application.LoadLevel(Application.loadedLevel) will be executed
if(GUI.Button(new Rect(Screen.width/2-40, Screen.height/2+10,80,20), "Play Again?"))
{
Application.LoadLevel(Application.loadedLevel);
}
}
}
}
Pickup script:
public class Pickups : MonoBehaviour
{
//Variables
bool isPickedUp = false;
// Use this for initialization
void Start ()
{
}
// Update is called once per frame
void Update ()
{
}
void OnTriggerEnter(Collider other)
{
if(other.gameObject.tag =="Player")
{
isPickedUp = true;
Debug.Log ("Picked up!");
StartCoroutine(RespawnItem());
}
isPickedUp = false;
}
//Respawn the pickup
IEnumerator RespawnItem()
{
if(isPickedUp)
{
int respawnTime = 3;
yield return new WaitForSeconds(respawnTime);
gameObject.SetActive(true);
Debug.Log ("Spawned!");
}
}
}