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 TheTurkeydipking · Jan 11, 2015 at 12:59 PM · arrayspawnclass

Spawning GameObject from class array Javascript

Hey hey Unity Anwsers! For a project we are trying to spawn a singular character from a list of 24 predefined characters. There is a code where we set the characteristics of the characters, with each variable being an enum (except for the character - GameObject)

 class Character {
 
 var gender : Gender;
 var hair : Hair;
 var pet : Pet;
 var item : Item;
 var clothes : Clothes;
 var character : GameObject;
 ..
 }

And there is now a second script where at random, it selects one member of the 24 character cast. The question is, how do we access the GameObject variable from the class in order to spawn it? The code we have so far is:

 var activeChar : boolean;
 
 function Start () {
 
 activeChar = false;
 
 var setChar = AllCharacters.Length;
 }
 
 function Update () {
 
 if(activeChar == false) {
     var randChar = Random.Range(0, AllCharacters.Length);
     Debug.Log (randChar);
     activeChar = true;
     }
     
 if(activeChar == true) {
 
     Instantiate(
 }
 }

Thanks a lot in advance!

Comment
Add comment · Show 3
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 incorrect · Jan 11, 2015 at 01:12 PM 0
Share

As I said, you can make prefabs for each character, organize an array of those prefabs and randomly pick from it.

The question is, how do we access the GameObject variable from the class in order to spawn it?

It can be done like this:

 Instantiate(characterInstance.character, Vector3.zero, Quaternion.identity);

But I guess that's not what you want us to tell you.

avatar image TheTurkeydipking · Jan 11, 2015 at 01:16 PM 0
Share

That's the thing :/ I know how to spawn it like this, but I am clueless on how to spawn only one of the 24 characters at random. Since every character is basically an element in the Unity Editor, I need to access one of those elements and the GameObject within it, correlating to a random number - and that's where Im utterly lost..

avatar image incorrect · Jan 11, 2015 at 01:41 PM 0
Share

Ok, so there are two ways: you can describe your problem more detailed, because what you've told us is not enough to help you, or you can pm me and i'll help you to make it right if we can use some messenger like skype. It's just too hard to help you because the answer was already given when I said that you just need to randomly pick from a collection of prefabs.

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by TheTurkeydipking · Jan 11, 2015 at 01:46 PM

I feel like I'm really close now; this is the code:

 #pragma strict
 
 var AllCharacters : Character[];
 
 var toSpawn : GameObject;
 
 var activeChar : boolean;
 
 function Start () {
 
 activeChar = false;
 
 var setChar = AllCharacters.Length;
 }
 
 function Update () {
 
 if(activeChar == false) {
     var randChar = Random.Range(0, AllCharacters.Length);
     Debug.Log (randChar);
     activeChar = true;
     
     }
     
 if(activeChar == true) {
     var x : int;
     x = randChar;
     Instantiate(AllCharacters.character[x], Vector3.zero, Quaternion.identity);
 }
 }

But Unity states "BCE0019: 'character' is not a member of 'Character[]'." which I don't understand since "character" is one of the variables of the "Character" class.. any ideas?

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 incorrect · Jan 11, 2015 at 01:50 PM 1
Share

Line 29, fix this:

 AllCharacters[x].character
avatar image TheTurkeydipking · Jan 11, 2015 at 01:57 PM 0
Share

I love you.

avatar image
1

Answer by Jason Neo · Jan 11, 2015 at 03:34 PM

Hmm let me give it a try,

 #pragma strict
 
 //In Inspector, drop all 24 characters here.
 public class AllCharacters[] ;
 
 var activeChar : boolean;
  
  function Start () {
  
  activeChar = false;
  
  var setChar = AllCharacters.Length;
  }
  
  function Update () {
  
  if(activeChar == false) {
      var randChar = Random.Range(0, AllCharacters.Length);
      Debug.Log (randChar);
      activeChar = true;
      }
      
 var randomCharacter = AllCharacters(Random.Range(0, 25));
  if(activeChar == true) {
  
      Instantiate(randomCharacter);
  }
  }

I'm new myself so I'm not sure if this will work :/

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 TheTurkeydipking · Jan 11, 2015 at 03:41 PM 0
Share

Though I've figured it out previously your solution seems a lot better! Thank you for submitting - I don't have a chance to try it out now but it looks great (:

avatar image Jason Neo · Jan 12, 2015 at 11:07 AM 0
Share

Happy that it worked out for you =) Good luck on your project!

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

27 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

Related Questions

[SOLVED]How to use attributes from array of custom class instances? 1 Answer

Spawning GameObjects with help of classes --- Attaching classes to GameObjects 1 Answer

How can i delete an item from an array after being used? (So, it won't be repeated) 2 Answers

Spawn carachters on locations and destroy the manager after level load 1 Answer

Creating a class wide array (c#) 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