Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 ungalyant2 · Sep 02, 2011 at 02:23 AM · variablesnullastar

Null Scripts

In an attempt to have some form of AStar pathfinding I have my enemies each referencing an AStar script, however, no matter how I initialise it, the referenced script always seems to be null, I have tried attaching it to the prefab and using:

AStar aStar = getComponent();

I've also tried

Astar astar = new AStar();

but no matter what I do, the variable remains null, it's infuriating because I swear I've done this before, but it just doesn't seem to be working this time, can anyone help me out here? Am I doing something stupid?

I'm using C# by the way.

Comment
Add comment · Show 20
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 Waz · Sep 02, 2011 at 02:26 AM 0
Share

Those code segments are not even syntactically correct. What did you actually try, and how did you know the result was null? An error message of some kind?

avatar image ungalyant2 · Sep 02, 2011 at 02:29 AM 0
Share

Wow, ok my bad, I must've just zoned out whilst typing that.

the first was Astar aStar = getComponent();

the second I believe was how I actually did it, I copied it from an A Star tutorial and it seemed to work for the guy.

avatar image ungalyant2 · Sep 02, 2011 at 02:30 AM 0
Share

ok so evidently I did do it right, but unity answers doesn't like greater than and less than symbols so I'll use commas ins$$anonymous$$d

Astar aStar = getComponent,AStar,();

avatar image Waz · Sep 02, 2011 at 02:40 AM 0
Share

You need to format as code (the 101010 button, or indent 4 spaces), then they show up.

avatar image ungalyant2 · Sep 02, 2011 at 02:44 AM 0
Share

Right, so what I entered was

 Astar astar = getComponent<AStar>();
Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
-1

Answer by aldonaletto · Sep 02, 2011 at 04:27 AM

If the script is attached to the same object, you should use:

AStar astar = GetComponent<AStar>();
But if the script is attached to another object, you need some reference to it. Supposing other is the object transform or collider:

AStar astar = other.GetComponent<AStar>();
Comment
Add comment · Show 3 · 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 ungalyant2 · Sep 02, 2011 at 11:46 AM 0
Share

that's the problem, I've done that, but it's still returning as null.

avatar image aldonaletto · Sep 02, 2011 at 03:39 PM 0
Share

What exactly are you trying to do? Usually we have to get a script via GetComponent when it's part of a different object - if you want to call some function or alter some variable in your enemies, for instance. In this case, you need a reference - collider, transform, gameObject, whatever - to the target object, like above. Another possibility is when you have several scripts attached to the same object, and one script must alter/call something in another script - there's no need for a reference, in this case.

avatar image ungalyant2 · Sep 02, 2011 at 03:45 PM 0
Share

The script is attached to the same prefab calling it, esentially I'm trying to call on variables and functions stored within the script to use in another script tied to the same prefab, I've been using

 AStar astar = GetComponent<AStar>();

but whenever I try to use the astar variable to call functions and references variables, it's always null.

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

7 People are following this question.

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

Related Questions

How to add multiple values to single variable(or something else)? 0 Answers

How to instantiate an object with scripts? 1 Answer

How to stop loading of a scene that's loading using SceneManager.LoadSceneAsync ? 1 Answer

What does this statement construction mean?: if (null != anim) 1 Answer

NullReferenceException MaskableGraphic.UpdateCull() upon Loading Player Data. 0 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