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 coolbird22 · Apr 09, 2014 at 12:33 PM · keypressinvokepoolingkeydown

On being Invoked by KeyPress, my gameobject appears alright, but does not get invoked again on key press ?

I was instantiating and destroying my gameobjects earlier but on learning that it is processor intensive, manipulated my code so that it is an object pool of 1 object. When I press Z, my object gets activated and starts scaling bigger as long as Z is pressed, and on key up, it deactivates the object, but when Z is pressed again, the object doesn't get Invoked again. What am I doing wrong ?

 float speed = 2f;
 public GameObject radius;
 public int pooledAmount = 1;
 List<GameObject> radiusorb;

 void Start(){
 
         radiusorb = new List<GameObject> ();
         for (int i = 0; i < pooledAmount; i++)
         {
             GameObject obj = (GameObject)Instantiate (radius);
             obj.SetActive(false);
             radiusorb.Add (obj);
         }
     }
     
     void Update (){
         if(Input.GetKeyDown(KeyCode.Z))
         {
             Invoke("LetRadiusExist",0.001f);            //Instantiating the prefab
                 }
 
         if (Input.GetKey(KeyCode.Z)) {                    //Scale the object
             if (gameObject != null)
             {
                 gameObject.transform.localScale += gameObject.transform.localScale * (Time.deltaTime * speed);
             }
         }
 
         if(Input.GetKeyUp(KeyCode.Z))                        //To deactivate the prefab
         {
             gameObject.SetActive(false);
 
             //Destroy(cloneRadius, 0);
         }
     }
 
     void LetRadiusExist()
     {
         for (int i = 0; i < radiusorb.Count; i++) 
         {
             if (!radiusorb[i].activeInHierarchy)
             {
                 radiusorb[i].SetActive(true);
                 radiusorb[i].transform.parent = transform;
                 radiusorb[i].transform.localPosition = new Vector3(0,0,0);
                 break;
             }
 
         }
     }
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

1 Reply

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

Answer by KiraSensei · Apr 09, 2014 at 03:40 PM

 gameObject.SetActive(false);

is your error. When you deactivate a game object, all its components are deactivated (so your script is deactivated too). So when you hit the "z" key again, the script is not running anymore. You need to deactivate other game objects, not the one which runs this script.

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 coolbird22 · Apr 09, 2014 at 04:07 PM 0
Share

Oh ! That makes so much sense. But, I can't make out how to refer to the game object that has been invoked (& instantiated).

 radiusorb.SetActive(false);


This gives me the following error : Type System.Collections.Generic.List' does not contain a definition for SetActive' and no extension method SetActive' of type System.Collections.Generic.List' could be found (are you missing a using directive or an assembly reference?)

avatar image KiraSensei · Apr 09, 2014 at 05:00 PM 0
Share

Here you try to deactivate a whole list, but you can't, you need to take each game object in it and deactivate it.

 for (int i = 0; i < pooledAmount; i++)
 {
     GameObject currGO = radiusorb[i];
     currGO.SetActive(false);
 }
avatar image coolbird22 · Apr 09, 2014 at 05:38 PM 0
Share

I tried doing this but I get an error saying that 'currGO' does not exist in the current context (currGO.SetActive(false);).

 if(Input.Get$$anonymous$$eyUp($$anonymous$$eyCode.Z))                        //To deactivate the prefab
         {
             currGO.SetActive(false);
 
             //Destroy(cloneRadius, 0);
         }
     }
     
     void LetRadiusExist()
     {
         for (int i = 0; i < radiusorb.Count; i++) 
         {
             if (!radiusorb[i].activeInHierarchy)
             {
                 GameObject currGO = radiusorb[i];
                 currGO.SetActive(true);
                 currGO.transform.parent = transform;
                 currGO.transform.localPosition = new Vector3(0,0,0);
                 break;
             }
         }
     }
avatar image KiraSensei · Apr 09, 2014 at 07:10 PM 1
Share

You didn't define it :

 if(Input.Get$$anonymous$$eyUp($$anonymous$$eyCode.Z)) 
 {
     for (int i = 0; i < pooledAmount; i++)
     {
         GameObject currGO = radiusorb[i];
         currGO.SetActive(false);
     }
 }
avatar image coolbird22 · Apr 09, 2014 at 07:42 PM 0
Share

That worked like a charm. I thought I was supposed to define it inside the LetRadiusExist function. Great to have learnt something new today. Thanks a lot $$anonymous$$iraSensei !

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

22 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

Related Questions

How to Code Obstacle to Go Back Into My Object Pooler When They Collide With a Boundary? 0 Answers

Key reading from inputString, backspace only works the first time 4 Answers

Forcing GetKeyDown by script. 2 Answers

Moving ship to a point on keydown 2 Answers

Checking if a method with a certain name exists through code! v 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