- Home /
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!
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.
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..
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.
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?
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 :/
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 (:
Happy that it worked out for you =) Good luck on your project!
Your answer
Follow this Question
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