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 Payaso_Prince · Jul 31, 2020 at 02:34 AM · instantiateprefaboverload

Instantiate a class that takes an overload?

Hello all!

I have a class called HeroUnit that takes a BattleManager as an argument. In the BattleManager class, I am instantiating Heros from an array, however, I cant seem to figure out where to pass it a battlemanager object. I want to pass it the battlemanager object that its currently being instatiated from with 'this'. The weird part is that this code creates the Gameobjects that have the HeroUnit script on it just fine and VisualStudio doesnt complain about them not taking a BattleManager as an argument. Any help would be MUCH appreciated! Here is an example:

  public  class HeroUnit : Unit
     {
         BattleManager battlemanager;

 public HeroUnit(BattleManager newBattleManager)
 {
     battlemanager = newBattleManager;
 }

 }

 public class BattleManager
 {
 public HeroUnit playerUnit;

  public void BattleSetup()
     {
         Debug.Log("Creating Units now!");
 
         
 
         // Create Heros  at their spawn points and store them into variables.
         CreateHeroes();
 
 
         // Access the HeroUnit information on the Hero GameObjects and store them into variables.
         playerUnit = createdHeroes[0].GetComponent<HeroUnit>();
 
 
     }
 
 
     void CreateHeroes()
     {
         
         
        if(party.currentParty.Count <= 3)
         {
             createdHeroes = new GameObject[party.currentParty.Count];
 
             for (int i = 0; i < party.currentParty.Count; i++)
             {
                 createdHeroes[i] = Instantiate(party.currentParty[i], heroLocations[i]);
                
             }
         }
         
     }
 
 
 }
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
Best Answer

Answer by xxmariofer · Jul 31, 2020 at 08:06 AM

if the HeroUnit is attached to the gameobject it means thaat unit derives from monobehaviour and if thats the case you cant use constructors, they get called multiple times when the gameobjects get serialize/deserialize, just avoid it and create an init method

  public  class HeroUnit : Unit
  {
      BattleManager battlemanager
      public void Init(BattleManager newBattleManager)
      {
           battlemanager = newBattleManager;
      }
  }

and when instantiate

          for (int i = 0; i < party.currentParty.Count; i++)
          {
              createdHeroes[i] = Instantiate(party.currentParty[i], heroLocations[i]);
              createHeroes[i].GetComponent<HeroUnit >().Init(this);
          }
Comment
Add comment · Show 5 · 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 Payaso_Prince · Jul 31, 2020 at 01:28 PM 0
Share

Thanks for the response man!

I'm not familiar with Init methods. I tried looking into it a good bit. It looks like they essentially work the same way as a constructor would?

I tried implementing it the way you did and was unsuccessful... Unity doesn't seem to recognize the word Init as a reserved term. It's kinda treating it like a generic method and asking for a return type. Any idea? Thanks again!

avatar image xxmariofer Payaso_Prince · Jul 31, 2020 at 02:07 PM 0
Share

i completly missed, i am talking about Init as a generic method i simply forgot to add the return type

avatar image Payaso_Prince xxmariofer · Jul 31, 2020 at 02:46 PM 0
Share

Hmm maybe I am misunderstanding?

When I attempt to get the HeroUnit component on my created Hero Gameobjects, I do it this way:

 playerUnit[i] = createdHeroes[i].GetComponent<HeroUnit>();

However, when I add the Init Function like this:

 playerUnit = createdHeroes[0].GetComponent<HeroUnit>().Init(this);

It complains about not being able to convert Void into HeroUnit.

feeling pretty confused on this...

Show more comments

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

187 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

Related Questions

Prefabs dont instantiate directly on point 0 Answers

Referencing gameObject from script after Instantiate 0 Answers

Prefab of a whole scene? 3 Answers

Dynamical prefab, how to instantiate + positioning 1 Answer

Infinity runner - best way to spawn board in runtime. 0 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