- Home /
How do I create separate instances from a class?
Well, I thought I would try to be clever and create a class for my score popup which I could instantiate at any time through a single line of code. That will save time right? Well , I use following line of code to trigger a score popup (a little +200 above the gameObject), but it seems that I can only trigger one of these at a time.(ie the first object has to finish its anim before the next object can trigger a popup) I kind of thought that the class would be newly instantiated every time an object called the TriggerPopUp method so I could have hundreds of these things at once. How can I do that?
TriggerPopUp("+200",gameObject);
And the code for this class is here:
#pragma strict
import TMPro;
/*
Call this using TriggerPopUp("+200",SenderGameObject);
*/
public static class ScorePopup extends MonoBehaviour{
var targetObject : Transform; //the 3d object5 to align to
var score :RectTransform;
var canvasRectT : RectTransform ;
var textMesh : TextMeshProUGUI ;
var popUpScore :GameObject;
var popUpExists : boolean = false;
function Update () {
}
//always position the score at the parent obejct
function PositionUI()
{
print("POSITIONING:");
var screenPoint :Vector2 = RectTransformUtility.WorldToScreenPoint(Camera.main,targetObject.position);
score.anchoredPosition = screenPoint - canvasRectT.sizeDelta / 2f;
}
//make the score visible and animate it
function TriggerPopUp(textToDisplay : String,go:GameObject)
{
//print("Clicked in class");
if(popUpExists){ return;}
//create pop up score from prefab
popUpScore = Instantiate(Resources.Load("PopUpScore") , Vector3 (0, 0, 0), Quaternion.identity);
popUpScore.transform.parent = go.transform;
popUpScore.name = popUpScore.name.Replace("(Clone)","");
targetObject=go.transform;
canvasRectT = go.transform.FindChild("PopUpScore/").gameObject.GetComponent(RectTransform);
score = go.transform.FindChild("PopUpScore/Score Text").gameObject.GetComponent(RectTransform);
textMesh = score.gameObject.GetComponent(TextMeshProUGUI);
yield WaitForSeconds(0.01);
PositionUI();
if(popUpExists){ return;}
//var reset =true;
popUpExists =true;
textMesh.text=""+textToDisplay;
textMesh.color.a=1;
score.gameObject.transform.localScale=Vector3(1,1,1);
PositionUI();
//animate
iTween.ScaleBy(score.gameObject,iTween.Hash("x",1.5,"y",1.5,"time",0.5));
yield WaitForSeconds(0.5);
PositionUI();
//fade out
while (textMesh.color.a>0 )
{
textMesh.color.a-=0.03;
yield;
}
popUpExists =false;
Destroy(popUpScore);
}
}
Cheers
Answer by Mark Gossage · Jun 08, 2015 at 03:00 PM
Ok, I'm not clear on what the code does (don't know about the TMPro stuff), but here I what I can see.
Its a static class. Which has a set of static functions & variables. Therefore you can only have one at a time (thats what static is all about).
At line 57, there is the 'if(popUpExists){ return;}' which will stop multiple sets running at once.
What I would do to fix it is to change from a sole static object to an object pool (check the Unity3d.com/learn there is an example of that). That will solve your problem.
Answer by LinkoVitch · Jun 08, 2015 at 02:44 PM
You have created a static class, these cannot be instantiated:
https://msdn.microsoft.com/en-us/library/79b3xss3.aspx
HTH
Your answer
![](https://koobas.hobune.stream/wayback/20220613200750im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Class enemy in javascript help 1 Answer
Defining a Javascript Class 1 Answer
Default References Problem 1 Answer