- Home /
Script wont read array
i have a class with 4 variables. Another script puts this class into an array. the game wont read the array unless i minimize the screen the click on the array in the editor. Is this a glitch in the engine or did i do something wrong. Everything is marked public.
this is the class script
#pragma strict
public class ablities2 {
public var name:String;
public var pic:Texture2D;
public var description:String;
public var id:int;
public var HaveAblity:boolean;
}
This is the script that reads the class and puts it into an array
In the update i try to see if it reads the array. I entered the values in the inspector manually so i know ClassAbilties[0] exist but the script will not read it unless i minimize the screen then click the array ClassAbilties in the inspector
public var RaceAbilties:ablities2[];
public var ClassAbilties:ablities2[];
public var PersonalityAbilties:ablities2[];
public var SpecialityAbilties:ablities2[];
public var Classes:Race_Combat_Class;
var SlashPicUnlocked:Texture2D;
var SlashPicLocked:Texture2D;
var Slot1:String;
var Slot2:String;
var Slot3:String;
var Slot4:String;
function Awake(){
Classes=transform.GetComponent(Race_Combat_Class);
ClassAbilties= new ablities2[5];
}
function Update() {
if(ClassAbilties[0]){
print("the program is reading this");
GiveSkillLine();
}
}
function GiveSkillLine(){
if(Classes.Warrior){
//ablity 1
ClassAbilties[0].name="Leap Slash";
ClassAbilties[0].description="Jump Above The Enemy And Slashes Down";
if(ClassAbilties[0].HaveAblity){
ClassAbilties[0].pic=SlashPicUnlocked;
}else{
ClassAbilties[0].pic=SlashPicLocked;
}
//ablity 2
ClassAbilties[1].name="Double Slash";
ClassAbilties[1].description="Slash The Enemy Twice";
if(ClassAbilties[1].HaveAblity){
ClassAbilties[1].pic=SlashPicUnlocked;
}else{
ClassAbilties[1].pic=SlashPicLocked;
}
}
if(Classes.Lair){
//ablity 2
PersonalityAbilties[0].name="Lie";
PersonalityAbilties[0].description="Lie to enemy ";
if(PersonalityAbilties[0].HaveAblity){
PersonalityAbilties[0].pic=SlashPicUnlocked;
}else{
PersonalityAbilties[0].pic=SlashPicLocked;
}
}
}
Answer by rasheedqw · Dec 26, 2014 at 04:38 PM
The Problem was line 25 of the second script ClassAbilties= new ablities2[5];. I dont Know why but it makes the array unreadable.I remove the line and it fix everything. If someoone can please tell me why this happens would love to know why thank you
Answer by Landern · Dec 26, 2014 at 02:03 PM
You need the class to serialize correctly, that means if it's directly(drag and drop style) attached to a GameObject, you should derive(extend) from MonoBehaviour, if it's not going to be directly attached to a GameObject, like you're doing in your example, ScriptableObject is the way to go:
public class ablities2 extends ScriptableObject {
public var name:String;
public var pic:Texture2D;
public var description:String;
public var id:int;
public var HaveAblity:boolean;
}
You may also want to look into using List's instead of array's as you can manipulate them in code with ease.
didnt know you could put class scripts on gameobject it only let me do that with c sharp scripts found the problem seems to be line 25 of the second script ClassAbilties= new ablities2[5]; dont really understand why but when i removed it everything worked fine. I use arrays because i understand them better. thanks for the info though
@rasheedqw, well, in the Awake function, when you do:
ClassAbilties= new ablities2[5];
This will remove anything you did in the inspector as it reset the ClassAbilities variable to a capacity of 5 abilities items in the array.
Your answer
Follow this Question
Related Questions
Accesing custom class object from another script 1 Answer
Creating a class wide array (c#) 1 Answer
Different way to access class variables? 1 Answer
Not a member of Object..Can't get class.name from array 1 Answer
problem with FindWithTag 2 Answers