Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 /
  • Help Room /
avatar image
0
Question by zigrasss · Dec 16, 2020 at 12:09 PM · scripting probleminstancecreateinstances

Is creating new Instances of the same class repeatedly instead of reusing them a problem?

I am working on a Battle arena game, and i am following a tutorial i found on Youtube in regards to creating an ability factory (i'll leave link below in case)

In the tutorials there an AbilityFactory script, which gets called into every time an ability is used. and it look like this

 public static Ability GetAbility(string abilityType)
 {
           if (abilititiesByName.CointainsKey(abilityType))
           {
                   Type type = abilitiesByName[abilityType];
                   var ability = Activator.CreateInstance(type) as Ability;
                   return ability;
           }
           return null;
 }

the GetAbility function use Activator.CreateInstance, which as far as i am concerned will create a new instance of the scripts every time an ability it used, is this the case? and if so, is there any problems associated with this way of using abilities. especially in regard to performance, if a lot of abilities is being used during a game.

And is there any better alternative to this, if this way causes problems?

link to video: https://www.youtube.com/watch?v=FGVkio4bnPQ&t=755s&ab_channel=JasonWeimann

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

Answer by xxmariofer · Dec 16, 2020 at 12:36 PM

Obviously if you can store and reuse the "Abilities" would be better. And knowing if it will cause problems is imposible without knowing the full game and seeing the full code, if you are creating abilities every frame and storing them forever it will end up giving issues, but in a game in which you create an ability once and you have multiple abilitytypes storing them would be worse

Comment
Add comment · Show 2 · 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 zigrasss · Dec 16, 2020 at 01:30 PM 0
Share

The way it works, is each ability i considered an AbilityBehaviour so each individual AbilityBehaviour will have it's own script that inherits from a scripts called AbilityBehaviour, and each individual AbilityBehaviour will be stored in a dictionary in the AbilityFactory.

the problem i'm having is that i don't know how to store a reference to each AbilityBehaviour script in the AbilityFactory. i'm getting all the scripts using "Assembly.GetAssembly", and then i wan't to loop through this list, creating an instance of each AbilityBehaviour script, and store a reference to each scripts in the dictionary.

I know i can use "Activator.CreateInstance" to loop through and and create an instance of each AbilityBehaviour, however i don't know how to then add a reference of it to the dictionary with it's associated name. in this case i want the key of the dictionary to be a string, witch is the unique name found in each AbilityBehaviour scrips, and the value to be a reference to the script. so i call a function from within a specific AbilityBehaviour scripts, when i want to use the ability.

 public static class AbilityBehaviours
 {
     static Dictionary<string, Type> abilityBehaviours;
     static bool isInstialized => abilityBehaviours != null;
 
     static void InitializeFactory()
     {
         if(isInstialized) { return; }
 
         var abilityBehaviourList = Assembly.GetAssembly(typeof(AbilityBehaviour)).GetTypes().Where(myType => myType.IsClass && !myType.IsAbstract && myType.IsSubclassOf(typeof(AbilityBehaviour)));
 
         abilityBehaviours = new Dictionary<string, Type>();
         
         //dont know how to do this?
         foreach(var type in abilityBehaviourList)
         {
             var tempBehaviour = Activator.CreateInstance(type) as AbilityBehaviour;
 
             abilityBehaviours.Add()
         }
     }
 }


 public abstract class AbilityBehaviour
 {
     public abstract string Behaviourname { get; }
 
     public abstract void UseAbility();
 }


avatar image xxmariofer zigrasss · Dec 17, 2020 at 08:23 AM 0
Share

I am not sure if I understood your full issue, are you asking about somehting like this?

      abilityBehaviours = new Dictionary<string, Type>();
      
      //dont know how to do this?
      foreach(var type in abilityBehaviourList)
      {
          var tempBehaviour = Activator.CreateInstance(type) as AbilityBehaviour;
 
          abilityBehaviours.Add(tempBehaviour.Behaviorname, tempBehaviour)
      }

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

326 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 Fix this issue 1 Answer

"instance" unexpected symbol? 1 Answer

Object reference not set to an instance of an object [problem with Unity 5] 2 Answers

Object reference not set to instance for my timer and my text 0 Answers

Why do I keep getting this error? 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