Hi!
I am making a Star Empires game, and when you click on a object i use OnMouseDown() to have that object pass it self to a variable in my GuiScript. Then the GuiScript draws one button on the screen for each function that should be accesible. And for this i have previously used the StartCoroutine(String, argument) version. But now reading that that on was slower and could only hold one argument i want to change to StartCoroutine(function(argument)). I did so and it works but gives error NullReferenceException. Why is this?
Version that works
class ShipScript(MonoBehaviour):
Variables!
def Start ():
// player1 = GameObject.Find('player1')
player1GuiScript = GameObject.Find('player1').GetComponent[of GuiScript]()
gameRules = GetComponent[of GameRules]()
def OnMouseDown():
if Input.GetButtonDown("Fire1"):
player1GuiScript.SetSelected(self)
def Actions():
return [{"name" : "IncreaseArmor", "requireMouseClick" : 0},{"name" : "Move", "requireMouseClick" : 1}]
def IncreaseArmor():
armor= armor+100
class GuiScript(MonoBehaviour):
selected as duck
def OnGUI():
if selected != null:
// loop through all actions of the selected object
functions as duck = selected.Actions()
for index as int in range(len(functions)):
// Create a new button for each action
if (GUI.Button (Rect (20,70+buttonWidth+2,80,20), functions[index]["name"] as System.String ) ):
// Check if the function need mouse click input
if(functions[index]["requireMouseClick"] != 0 ):
// If so mark this and store the function
SetNeededMouseClicks(functions[index]["requireMouseClick"])
waitingFunction["name"] = functions[index]["name"]
waitingFunction["object"]=selected
else:
// Else call the function as is
selected.StartCoroutine(functions[index]["name"],null)
buttonWidth=buttonWidth+20
This is the important part
selected.StartCoroutine(functions[index][“name”],null)
With the version that don’t work i have changed the following.
class ShipScript(MonoBehaviour):
def Actions():
return [{"name" : IncreaseArmor, "requireMouseClick" : 0},{"name" : "Move", "requireMouseClick" : 1}]
class GuiScript(MonoBehaviour):
selected.StartCoroutine(functions[index]["name"](null))
And this gives the error:
NullReferenceException
UnityEngine.MonoBehaviour.StartCoroutine (System.Collections.IEnumerator) (at C:/BuildAgent/work/6bc5f79e0a4296d6/Runtime/ExportGenerated/Editor/BaseClass.cs:1067)
(wrapper dynamic-method) ShipScript.ShipScript$StartCoroutine$ (object,object[]) <IL 0x00012, 0x00100>
Boo.Lang.Runtime.RuntimeServices.Dispatch (object,string,System.Type[],object[],Boo.Lang.Runtime.DynamicDispatching.DispatcherCache/DispatcherFactory) <IL 0x0002e, 0x0012e>
Boo.Lang.Runtime.RuntimeServices.Dispatch (object,string,object[],Boo.Lang.Runtime.DynamicDispatching.DispatcherCache/DispatcherFactory) <IL 0x0000c, 0x00064>
Boo.Lang.Runtime.RuntimeServices.Invoke (object,string,object[]) <IL 0x00039, 0x000f1>
GuiScript.OnGUI () (at Assets/Game scripts/GUI/GuiScript.boo:89)
The error is one the row
selected.StartCoroutine(functions[index]“name”)
in GuiScript
Anyone got any idea why this is happening? It still works, it execute the function when i press the button, but it generates the error.