Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by William_Weaver · May 12, 2014 at 03:02 AM · instantiatebuild-errornullreferenceexceptionsendmessageagent

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;
 }
Comment
Add comment · Show 3
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image snarf · May 12, 2014 at 10:48 AM 0
Share

owner.GetChild(1).Find("HandRight"); <--- please check this

avatar image William_Weaver · May 12, 2014 at 10:27 PM 0
Share

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.

avatar image William_Weaver · May 12, 2014 at 10:30 PM 0
Share

^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.

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

21 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Instantinate NullReferenceError problem 0 Answers

Instantiated object gives NullReferenceException 1 Answer

Material[] Object reference not set when instantiating 2 Answers

Variable Assigning with GameObject.Find("") causing NullReferenceException? 0 Answers

Building for android freezes unity editor 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges