• 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 grimmy · Jun 08, 2015 at 01:13 PM · class

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 w$$anonymous$$ch 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 t$$anonymous$$ngs at once. How can I do that?

 TriggerPopUp("+200",gameObject);

And the code for t$$anonymous$$s class is here:

 #pragma strict
 import TMPro;
 
 /*
 Call t$$anonymous$$s 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.FindC$$anonymous$$ld("PopUpScore/").gameObject.GetComponent(RectTransform);
         score = go.transform.FindC$$anonymous$$ld("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
         w$$anonymous$$le (textMesh.color.a>0 )
         {
             textMesh.color.a-=0.03;
             yield;
             
         }
         popUpExists =false;
         Destroy(popUpScore);
         
      }
 
  }

Cheers

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

2 Replies

· Add your reply
  • Sort: 
avatar image
1

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. W$$anonymous$$ch 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;}' w$$anonymous$$ch 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.

Comment
Add comment · 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
0

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

Comment
Add comment · 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

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

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 do I show a Classes Vars in the inspector (Javascript)?? 2 Answers

Accessing another script from a class 3 Answers

Class Object Array? 1 Answer

How to make classes in C# that are assignable to public variables in scripts trough the Editor 1 Answer

function Start() and class in class 3 Answers


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