- Home /
Functions work in the Editor, don't work when Built. (Javascript)
I doubt I'll get an answer, or even the question approved in time, but still.. There's a lot that goes into this, so long story short:
I click a button from OnGUI in an inventory script attached the the player. The item (a sword in this case) for that slot is instantiated using Resources.Load, and SendMessage("Use", this.gameObject) is sent to the item just created.
The item's function Use(agent : GameObject) passes the agent information through it's script to various other functions, making the sword a child of the player, moving it's position/rotation to match an empty that's part of the player, etc.
From there I've got functions, all code, that move the sword to a ready position, swing the sword, and return to stowed. It all works wonderfully in Unity. Soon as I build it, to any platform, it starts failing:
NullReferenceException: Object reference not set to an instance of an object at 1HandMeleeControl.StowPosition (UnityEngine.GameObject agent) [0x00000] in :0
at 1HandMeleeControl.Use (UnityEngine.GameObject agent) [0x00000] in :0 UnityEngine.GameObject:SendMessage(String, Object, SendMessageOptions) UnityEngine.GameObject:SendMessage(String, Object) Inventory:InstanceUse(Item) Inventory:OnGUI()
Here's the actual code in question:
public function Use(agent : Transform)
{
owner = agent;
// Debug.Log("set owener to agent");
// Debug.Log("Use Called with "+agent.name);
var playerScript = owner.gameObject.GetComponent(PlayerScript);
// Debug.Log("Passed playerScript find");
rightHand = owner.GetChild(1).Find("HandRight");
// Debug.Log("passed rightHand value");
rHStartPos = rightHand.localPosition;
rHStartRot = rightHand.localRotation;
rHStartSca = rightHand.localScale;
// Debug.Log("passed rightHand properties");
playerScript.EquipItem(this.transform, stats);
// Debug.Log("passed "+agent.name+" to EquipItem");
StowPosition();
// Debug.Log("Called StowPosition with "+agent.name);
}
function StowPosition() : void{
var stowPos = owner.GetChild(1).transform.Find("1HStowed");
// Debug.Log("Passed stowPos value");
transform.parent = stowPos;
// Debug.Log("parented sword to stowPos");
transform.localPosition = Vector3.zero;
transform.localRotation = Quaternion.Euler(0,0,0);
// Debug.Log("set sword pos/rot to stowPos");
state = 1;
// Debug.Log("set wpn state to 1");
this.gameObject.layer = LayerMask.NameToLayer("Player");
// Debug.Log("set sword layer to player");
for(var child : int = 0; child < this.transform.childCount; child++)
{
this.transform.GetChild(child).gameObject.layer = LayerMask.NameToLayer("Player");
}
// Debug.Log("set sword child objects layers to player");
swingPivot = owner.transform.GetChild(1).transform.Find("MeleePivot");
// Debug.Log("set swingPivot value to MeleePivot");
readyPos = swingPivot.transform.Find("1HReady");
// Debug.Log("set readyPos");
resetReadyRot = readyPos.localRotation;
// Debug.Log("set resetReadyRot");
rightHand.parent = owner.transform.GetChild(1);
rightHand.localPosition = rHStartPos;
rightHand.localRotation = rHStartRot;
rightHand.localScale = rHStartSca;
}
owner.GetChild(1).Find("HandRight"); <--- please check this
I saw in the Debug_log that it was having issues in those areas. I'm currently trying to find alternative ways to work that function and testing them out. So far every change I've made has has the same results: Works in Unity, doesn't work in the build.
I need the code to work in this way (for now, since I have yet to get into animation and don't have a rig to reference ins$$anonymous$$d) because 'owner' is a player, and the player can change forms. I've just named each form "PlayerFormName" so they're always in child position (1) when the player controller running the script instantiates them and makes them a child of itself.
^That said, I think I'll give all the player models the same name since they contain a form class variable that holds their stats and try referencing them by name rather than GetChild. gameObject.Find seems to have no issue.