Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 paezrice · Oct 06, 2017 at 09:32 AM · instantiateprefabsarraysprefab-instanceinstantiation

Instantiating Prefabs from a MonoBehaviour-derived class using an array -> compiling but not rendering [SOLVED]

Hey guys, so i thought i'd spawn 4 controllable characters (in the code snippet i named them players, sry for the inconvenience) for 2 different players. I try to instantiate them in my GameManager's for-loop, but they don't spawn neither in the game nor are listed in the hierarchy. Before going any further, here is the code:

     public HumanPlayer[] prefabPlayer1;
     public HumanPlayer[] prefabPlayer2;
 
     private void Awake()
     {
         prefabPlayer1 = new HumanPlayer[4];
         prefabPlayer2 = new HumanPlayer[4];
         SpawnPlayerOne();
         SpawnPlayerTwo();
     }
 
  private void SpawnPlayerOne()
     {
         for(int i = 0; i < prefabPlayer1.Length; i++)
         {
             //creating new Player for the user (i times)
             prefabPlayer1[i] = new HumanPlayer();
             prefabPlayer1[i].SetPlayerType(i);
             Instantiate(prefabPlayer1[i], new Vector3(i,2,i), Quaternion.identity);
         }
     }
 
     private void SpawnPlayerTwo()
     {
         for(int i = 4; i < 8; i++)
         {
             prefabPlayer2[i%4] = new HumanPlayer();
             prefabPlayer2[i%4].SetPlayerType(i%4);
             Instantiate(prefabPlayer2[i%4], new Vector3(i,2,i), Quaternion.identity);
         }
     }

note that HumanPlayer is a class derived from my abstract PlayerScript class (that's derived from MonoBehaviour). VS2017 doesn't show me any errors on this code. Also the HumanPlayer script is attached to my Player prefab (which also has a MeshFilter and MeshRenderer) and the GameManager is tied to an "Empty" in my scene (that's also active ofc). My GameManager shows me the following arrays: alt text

i thought the "None" only means, they got no specific name assigned to them. However since they don't spawn at all, i dunno.

unity-answers.png (13.9 kB)
Comment
Add comment · Show 6
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 NeonTheCoder · Oct 06, 2017 at 09:37 AM 1
Share

Try something like prefabPlayer1[i] = Instantiate(new HumanPlayer, new Vector3(i,2,i), Quaternion.identity)

avatar image paezrice NeonTheCoder · Oct 06, 2017 at 09:47 AM 0
Share

this didn't resolve the issue :( however it's a much cleaner way to write it, so i changed it, ty :)

avatar image NeonTheCoder paezrice · Oct 06, 2017 at 09:56 AM 1
Share

$$anonymous$$aybe it has something to do with your HumanPlayer class? Are there any other parameters the Constructor could take? Is HumanPlayer a gameObject/prefab? If not try instantiating a new gameObject and adding the HumanPlayer script. example:

   public GameObject[] prefabPlayer1;
 //...
  ...//
   private void SpawnPlayerOne()
      {
          for(int i = 0; i < prefabPlayer1.Length; i++)
          {
              //creating new Player for the user (i times)
              prefabPlayer1[i] = Instantiate(new GameObject(), new Vector3(i,2,i), Quaternion.identity);
              prefabPlayer[i].AddComponent<HumanPlayer>();
              HumanPlayer pS = prefabPlayer[i].GetComponent<HumanPlayer>();
              pS.SetPlayerType(i);
              
          }
      }
Show more comments

0 Replies

· Add your reply
  • Sort: 

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

89 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

Related Questions

Need help understanding scripted prefab behavior - when I click one, the script runs on ALL prefab instances, not just the one I clicked. 3 Answers

How to manipulate a variable of a prefab script (instantiated) while the game is runnning . 1 Answer

Accessing variable of instantiated prefab inside array in javascript 1 Answer

Clicker game instantiate prefab depending on gold per click 0 Answers

Do I necessarily need to instantiate a prefab before putting it in List to conserve his variable data? 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