- Home /
Creating an array of structs
Hi,
We want to create a priority system for some of our game objects to act in a battle scene and we encountered the following problem:
Our gameobjects have a script called stats and this script calls a GetPriorities function on object spawn. This GetPriorities function exists in another script called TurnSystem which is an empty object's component that is supposed to manage the turns of action of the objects.
The Stats script is:
public class Stats : MonoBehaviour { public int Level; public int HP; public int Damage; public TurnSystem system; public int BattlePriority; // Use this for initialization void Start () {
}
// Update is called once per frame
void Update () {
}
void OnSpawned(){
BattlePriority=system.GetPriority(Level,this.gameObject); <--line 21
}
}
The turnsystem script is:
using UnityEngine; using System.Collections;
public class TurnSystem : MonoBehaviour {
public class Turns {
private int priority;
private GameObject go;
public int Priority{
get{
return priority;
}
set{
priority=value;
}
}
public GameObject Go{
get{
return go;
}
set{
go=value;
}
}
}
public int i; //Array ordering int, needs reseting after each battle
public Turns[] BattleTurns=new Turns[10];
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
public int GetPriority(int a,GameObject go){
int priority;
priority=a*100+a*100*((Random.Range(-20,20))/100); //Priority Setting function, needs testing
SetPriority(priority,go);
return priority;
}
public void SetPriority(int b,GameObject went){
BattleTurns[i].Priority=b; <--line 51
BattleTurns[i].Go=went;
i++;
}
}
When we try to run it, Unity gives the following error:
NullReferenceException: Object reference not set to an instance of an object TurnSystem.SetPriority (Int32 b, UnityEngine.GameObject went) (at Assets/Scripts/TurnSystem.cs:51) TurnSystem.GetPriority (Int32 a, UnityEngine.GameObject go) (at Assets/Scripts/TurnSystem.cs:45) Stats.OnSpawned () (at Assets/Scripts/Stats.cs:21)
I'm guessing something is wrong in the way we declare stuff on GetPriority but I can't find how to properly fix it...
Thanks in advance
"asked 20 $$anonymous$$utes ago by Jokeaccount"... hm. this leads me to:
"public int i;" WHAT?! AHAHAHAHA AHAHAHAHA AHAHAHAHAHAHAHAHHHAHAHA thump
on the off chance that this is serious:
you need to set BattleTurns[i] = new Turns() before trying to mess with it.
Loius, your answer gives back "cannot implicitly convert TurnSystem.Turns to TurnSystem.Turns[]"
$$anonymous$$ight wanna check if the code you suggest actually works before being all funny and stuff.
aw, lighten up. :D
"joke account" posted a class with a publicly visible member called 'i'. nerds should find that worth a chuckle at the very least :)
but it is a Turns and not a Turn, which is what i wrote. i'll give you that one. :)
Think I might create an account called pleasedontanswermyquestion
Your answer

Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Null reference when accessing GameObject in the Array(C#) 1 Answer
Vector3 resultant array sorting 2 Answers