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 MountDoomTeam · Mar 15, 2013 at 06:48 PM · arraynot working

help! I can't get an array from another script

at the end of the start function there is a function that says:

     var tunnels : tunnels = gameObject.GetComponent(tunnels);//name of script
     var nodes : Vector3[] = tunnels.nodes;//name of array

it returns... null reference exception-not set to instance of an object

and I can see in the editor prior to and during play, it clearly is an array with values. I just can't seem to get it.

I attempted to wait yield 10 seconds before even calling the lines to get the array, but it says null reference exception straightaway as it loads.

this also returned a null reference exception:

 if ( ntunnels.nodes[2] == null ){var test = Vector3.one;}

what am I doing wrong?

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 sdgd · Mar 30, 2013 at 12:16 PM 0
Share

do you have both scripts same type? both JC or C#?

avatar image Fattie · Mar 30, 2013 at 12:24 PM 0
Share

just to be clear, you $$anonymous$$UST CHANGE your variable name to something different. what about happyTunnels. do not use "tunnels".

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by robertbu · Mar 15, 2013 at 07:01 PM

Your variable name is the same name as your type. Assuming your script name was "tunnels" and assuming that the script was attached to the same game object as the code above you can do:

 var theTunnels : tunnels = gameObject.GetComponent(tunnels);
 var notes = theTunnels.notes;
Comment
Add comment · Show 7 · 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 MountDoomTeam · Mar 15, 2013 at 07:35 PM 0
Share

thank you, it didn't solve it, it still says there is a null reference exception. do you know why the error appears straight away even when there is a WaitForSeconds (8) at the top of the function?

avatar image roojerry · Mar 15, 2013 at 07:46 PM 0
Share

WaitForSeconds should only be used inside a coroutine. So its not working as you would expect in the Start() function

you want something like this:

 function WaitBeforeAssign () {
     // suspend execution for 8 seconds
     yield WaitForSeconds (8);
     var theTunnels : tunnels = gameObject.GetComponent(tunnels);
     var notes = theTunnels.notes;
 }


then call this from your Start() in place of the 2 assignment calls:

  StartCoroutine(WaitBeforeAssign()); 


try that and see if you still get the null reference on start

avatar image robertbu · Mar 15, 2013 at 08:11 PM 0
Share

Are you absolutely sure that is a tunnels script attached to the same game object as this script? Not having the script attached to the same game object will cause this error. If it is attached to a different game object, then you need to find that object first and then call GetComponent on that object.

avatar image whydoidoit · Mar 15, 2013 at 09:59 PM 1
Share

@brianruggieri - Start functions may have a yield statement in Javascript, because they are allowed to be coroutines automatically without starting them explicitly. In C# you have to declare them as IEnumerator Start

avatar image robertbu · Mar 16, 2013 at 02:43 AM 1
Share

Why don't you post both scripts here or open a new question with both scripts concerning why you are having trouble with your Vector3. From the little bit here, I cannot figure out why you are having trouble.

Show more comments

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

14 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

Related Questions

Smooth Mouse Script Problems 0 Answers

Raycast Having Pointless Errors 1 Answer

Array not working as expected 0 Answers

Unity opening fails 0 Answers

Array - 'transform' is not member of Object 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