Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 Jonas 1 · Jan 15, 2011 at 08:23 PM · nullreferenceexceptiongetcomponentstatictypepragma

NullReference Problem with pragma strict

I've the following problem with an UnityScript when using #pragma strict:

#pragma strict

public var SwordAniScript: AttackFlow;

function Start() { var SwordAniScript : AttackFlow =GameObject.Find("SwordAni").GetComponent(AttackFlow) as AttackFlow; }

function Update() { if (SwordAniScript.canAttack) { //do Code } }

When I compile the script I doesn't get any error or warning.

But when the script will be executed I get an error: NullReferenceException: Object reference not set to an instance of an object at this line "if (SwordAniScript.canAttack) ". The AttackFlow script is also written in JS and canAttack is declared as:

  public var canAttack: boolean = true ;

Can somebody help me with this annoying problem ?

Thank you in advance.

Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by Peter G · Jan 15, 2011 at 08:41 PM

First, to clarify what NullReference exceptions mean. A NullReference means that Unity looked for an object in a reference type (any class, but not a struct) and it found null instead of an instance of your object.

In your case, Unity checked SwordAniScript, and if found nothing there, or null. Well then why?

Your local declaration of SwordAniScript appears to be masking your global definition. So you are creating a new variable called SwordAniScript and then finding the component. In that case, you probably are finding the component you want, but you are assigning it to the wrong variable which will go out of scope as soon as your function returns. Remove the var keyword to access the global variable and that should solve your problem.

Or, if that doesn't work, there is also a chance that "SwordAni" doesn't have a AttackFlow script attached to it, or that you do not have an object in your scene that exactly matches your string, "SwordAni".

Comment
Add comment · Show 1 · 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 Jonas 1 · Jan 15, 2011 at 08:49 PM 0
Share

Remove the var keyword to access the global variable --> This solved it. Brilliant member and a really good explanation!

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

Still strugging with GetComponent and accessing other objects (C#) 2 Answers

Variables in components always returning null? 2 Answers

GetComponet() returning null with require component script 1 Answer

Type of a script 1 Answer

.getComponent() creates an Instance? 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