- Home /
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;
}
}
}
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.
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?)
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);
}
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;
}
}
}
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);
}
}
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
![](https://koobas.hobune.stream/wayback/20220613142325im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
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