Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by ytwithlove · Jul 26, 2014 at 09:09 AM · respawnactivegame objectpickups

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!");
         }    
 
     }
 }
 

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

3 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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;

Comment
Add comment · Show 5 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image ytwithlove · Jul 27, 2014 at 01:29 PM 0
Share

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'"

avatar image tyrike thompson · Jul 30, 2014 at 01:25 AM 0
Share

then try this on the same line : Pickup = other.gameObject; Pickup.SetActive(false);

should still work the same.

avatar image ytwithlove · Aug 01, 2014 at 11:21 PM 0
Share

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. :(

avatar image ytwithlove · Aug 01, 2014 at 11:32 PM 0
Share

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! ^_^

avatar image tyrike thompson · Aug 04, 2014 at 04:47 AM 0
Share

glad you figured it out :)

avatar image
0

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;
Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image ytwithlove · Jul 27, 2014 at 03:18 AM 0
Share

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?

avatar image
0

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.

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image ytwithlove · Jul 27, 2014 at 01:14 PM 0
Share

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.

avatar image SVKsuli · Jul 27, 2014 at 02:47 PM 0
Share

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 :)

avatar image ytwithlove · Jul 27, 2014 at 09:36 PM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

26 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges