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 Lynchpin · Jun 28, 2010 at 03:34 PM · javascriptnullreferenceexceptionclasses

How to add an object to a MonoBehaviour in JavaScript?

I have a CharacterController for a simple monster AI, and I have a separate class for an A* pathfinding implementation. I'm hoping that I could attach an instance of the A* class to the monster, but I keep getting the following error: "NullReferenceException: Object reference not set to an instance of an object"

I'm hoping to have the A* instances for every monster, since they all need to find their paths around the level and for performance reasons the pathfinding is propagated across multiple frames (my levels are huge randomly generated dungeons).

Since Unity's JavaScript implementation doesn't seem to have the features that I'm used to, I'm quite stumped - I couldn't find any good examples on how to do this either. Is it even possible with JavaScript, or should I start looking at C#?

Comment
Add comment · Show 1
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 Cyclops · Jun 28, 2010 at 05:58 PM 0
Share

@Lynchpin, glad it helped - feel free to use the handy checkmark symbol below. :)

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by Cyclops · Jun 28, 2010 at 04:02 PM

Javascript files in Unity automatically inherit from MonoBehavior, so yes, it does have that feature, you don't need to use C# (although I prefer C# :)

I'm not sure what steps you have taken, but starting from the beginning - once you have a JS script somewhere in the Assets directory (should see it in the Project view), then it is a Component, and needs to be attached to a GameObject. So if you have a Monster GameObject in your Scene, you would just drag/drop the script to it.

Once it's attached, then you have to make sure the script is properly referencing the Monster GameObject, depending on what you want to do. For instance, if you wanted it to move forward, you could say something like:

transform.position.x += 20;

Since the script is now a Component of the GameObject, that line references the GameObject's Transform.

If that doesn't help, then could you edit your Question to indicate what variable is getting a NullReferenceException?

Comment
Add comment · Show 2 · Share
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 Lynchpin · Jun 28, 2010 at 04:14 PM 0
Share

Oh wow, it worked! I guess I just thought about it in an over-complicated manner; I'm used to instancing classes as member variables and I'm still a novice when it comes to Unity.

Thank you!

avatar image Cyclops · Jun 28, 2010 at 04:27 PM 0
Share

@Lynchpin, yeah, Unity is powerful, but it does take a different $$anonymous$$dset (which I'm still working on). For instance, any public variables in your script - can be edited at runtime in the Editor. So you can tweak variables while the project is running, until the numbers are right (although you have to set them in the script, once you leave runtime everything resets).

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

No one has followed this question yet.

Related Questions

Why ExecuteInEditMode Always Causes NullReferenceException Errors Even In Clamping!! 2 Answers

Help with class in Javascript 1 Answer

Organizing variables in the inspector 5 Answers

Calling inherited constructor 3 Answers

Split() into a fixed length array? 1 Answer


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