- Home /
SetActive Not Responding
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 (http://answers.unity3d.com/questions/390990/gameobject-setactive-not-reactivating.html) 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!");
}
}
}
Answer by tyrike thompson · Jul 27, 2014 at 05:08 AM
It's probably because the pickup script is attached to the Pickup Item, so when the player touches it, it deactivate so the pickup script will not work. You can still do the same thing by :
Making a new variable at the beginning of the script
GameObject PickedUp;
void OnTriggerEnter(Collider other)
{
if(other.gameObject.tag =="Pickup")
{
PickedUp = 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());
}
}
and the respawn part :
//Respawn the pickup
// IEnumerator RespawnItem()
// {
// if(isPickedUp)
// {
// int respawnTime = 3;
// yield return new WaitForSeconds(respawnTime);
// PickedUp.SetActive(true);
// Debug.Log ("Spawned!");
// }
//
// isPickedUp = false;
// }
Remember this must be attached to the player;
Tried out the code and got an error for this line: PickedUp = other.gameObject.SetActive(false);
The error Unity has is "Cannot implicitly convert type void' to
UnityEngine.GameObject'"
then try this on the same line : Pickup = other.gameObject; Pickup.SetActive(false);
should still work the same.
Had to declare GameObject Pickup so the new error showing "Pickup is not part of the current context" would go away. Sadly this didn't work either. :(
Oh my dear god I figured it out!!!
Here's something new that happened. While running the scene and watching the console, I did notice Debug.Log was printing "Picked Up!" during a collision.
But when the 3 seconds have passed and the pickup should respawn, this error comes up: "NullReferenceException: Object reference not set to an instance of an object ThirdPersonController1+c__Iterator2.$$anonymous$$oveNext () (at Assets/Scripts/ThirdPersonController1.cs:90)"
Went to line 90 and changed "PickedUp.SetActive(true);"
to
"Pickup.SetActive(true);"
This FINALLY caused the pickup to respawn after 3 seconds in the same place.
Oh my god thank you for your help! And thank you everyone! I've had this problem for so many months and it's finally solved! ^_^
Answer by ironblock · Jul 26, 2014 at 09:44 AM
so the scripts are on the same object. make an empty gameobject with the script witch should activate the other one.
but you can also to enable and disable the collider and mesh renderer of the object in stead of disabeling the whole object.
IEnumerator RespawnItem()
{
if(isPickedUp)
{
int respawnTime = 3;
yield return new WaitForSeconds(respawnTime);
gameObject.GetComponent<meshRendederer>().enabled = true;
gameObject.GetComponent<collider>().enabled = true;
Debug.Log ("Spawned!");
}
}
i think you can pull it off with out the get component buy im on my laptop without an editor so i cant check.
gameObject.collider.enabled = true;
gameObject.meshRenderer.enabled = true;
Unfortunately this didn't work. Didn't have any errors in my code but the sphere still isn't showing up. Grrr....
Just to make sure I'm doing this correctly, I create an empty game object and attach the IEnumerator script to this? Or do I attach the pickup script to this?
Answer by SVKsuli · Jul 27, 2014 at 05:08 AM
why you dont destroy this sphere on pick up and after 3sec create it back, it can work better i think.
That's what I'm trying to figure out how to do - let me know if you have any suggestions on how to set this up.
when you pick up sphere you destroy it with Destroy(gameobject) and any empty gameobject on this position can have script with if gameobject ==null then Instantiate gameobject with yeld waitforseconds(3)... i think this can work and i hope you undrestant how i think it :)
It sounds a little confusing but I'll give it a try. $$anonymous$$ay come to make sense once I start to code things out. Thanks! :)
Your answer
Follow this Question
Related Questions
Javascript How to Activate/Deactivate GameObject Arrays 1 Answer
Gameobject Respawn 1 Answer
On trigger active object, else not c# 2 Answers
Enemy Respawn Bug 3 Answers
help with death zone/respawn 1 Answer