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

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

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.

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

Class enemy in javascript help 1 Answer

Defining a Javascript Class 1 Answer

Default References Problem 1 Answer

How do I make an instance of a class global??? 1 Answer

Calling functions from a class - Curious issue 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