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 MrSplosion · Aug 21, 2011 at 02:01 AM · 2dtexturearrayvariable

Accessing a Variable From Another Script

I'm simply trying to access an array of 2D textures and print "hi" if nothing is in the array slot.

In one script I have:

 function Update(){
     for(i = 0; i < 49; i++){
         if(GetComponent(HUDAndInventory).inventoryTextures[i] == null){
             print("hi");
         }
     }
 }

In the script "HUDAndInventory" there is a total of 50 textures in the "inventoryTextures" array. I just want to iterate through each texture slot and print "hi" if nothing is assigned to it. I get the error: NullReferenceException: Object reference not set to an instance of an object. Now I understand that this error means I'm trying to reference something that isn't their, but every thing im referencing and accessing is their. So why do I get this error and how can I fix it?

Thanks!

Comment
Add comment · Show 2
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 Dreamish · Aug 21, 2011 at 03:25 AM 0
Share

At what point do you assign the array in the other script? Since you're running this in Update() (which means every frame from the beginning of the use of your object) you need to have assigned the array in Start(). I would recommend making a testingbutton in OnGUI() for this code ins$$anonymous$$d.

avatar image Peter G · Aug 21, 2011 at 04:12 AM 0
Share

The error is probably co$$anonymous$$g from:

 GetComponent(HUDAndInventory)

returning null. Then you are trying to access a variable on a null object which you can't do. Hence the NR$$anonymous$$ $$anonymous$$ake sure that you have a HUDAndInventory script attached to your game object as well.

2 Replies

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

Answer by diddykonga · Aug 21, 2011 at 05:57 AM

Try this

 function Start(){
     HUDAndInventory hud = GetComponent<HUDAndInventory>();
 }

 function Update(){

     for(i = 0; i < hud.inventoryTextures.Count; i++){
         if(hud.inventoryTextures[i] == null){
             Debug.Log("No Texture Found");
         }
     }
 }

For this to succesfully work you need to have this script and the HUDAndInventory script on the same GameObject. Hope this helped :)

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

Answer by em2 · Aug 21, 2011 at 07:48 AM

Hi MrSplosion,

If you define your array as a static var outside of your actual function you will be able to access it from other scripts / functions in your project. This will make it globally available to access.

for example:

 static var yourArrayName:Array=new Array();

Hope this helps.

Cheers, Chris

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

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

PackTextures Horizontally? (1 Row) or Alternative Method 1 Answer

variable with int value not passing to command 1 Answer

Making an array for Texture2D?? 2 Answers

Array based floor 3D game flat plane 1 Answer

Trouble populating 2D array for Match-3 Game 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