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 Madswint · Jun 17, 2014 at 02:52 PM · arraywwwphp

Putting array nr. 1 in a string ;o?

I'm trying to make a playlist. It takes values from a php file, then it makes an array of all values in Unity, and finally it's supposed to take the first value, and add it to the end of a string, example:

String = "www.google.com/" + songNames[1]; songNames : String[];

It gives me this error?

 NullReferenceException: Object reference not set to an instance of an object
 Radio..ctor () (at Assets/[Mit] Scripts/Radio.js:4)
 

Here's my javascript.

 import System.IO;
 
 var getPlaylist : String = "http://shuleii.dk/Guardian/getfiles.php";
 var url : String = "http://shuleii.dk/Guardian/Playlist/" +songNames[1];
 var songNames : String[];

 
 function Start () {
 
 
 var www : WWW = new WWW (getPlaylist);
 yield www;
 songNames =  www.text.Split(","[0]);
 Debug.Log("Songs: " + www.text);
 
 
 }


Anyone know what I screwed up? Thank you! I'm almost finished with it then.

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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by tanoshimi · Jun 17, 2014 at 02:57 PM

You're trying to access songNames[1] on line 4, which is one line before you've declared the songNames variable...

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 Madswint · Jun 17, 2014 at 03:08 PM 0
Share

Problem is it takes time for it to get the php, so it tries to take the array 1 before. Which is what you said if I'm not wrong:>, anyways do you know any way to wait for the array to get the results, then add [1] to url? Thanks!

avatar image tanoshimi · Jun 17, 2014 at 03:20 PM 0
Share

No, that's not what I meant... Change line 4 to just declare the variable:

 var url : String;

Then assign its value at the end of the Start() function once the songNames data has been returned:

 ...
 Debug.Log("Songs: " + www.text);
 url = "http://shuleii.dk/Guardian/Playlist/" +songNames[1];
 }
avatar image
0

Answer by GrandOpener · Jun 17, 2014 at 03:35 PM

The problem, as tanoshini already described, is that you are trying to access the songNames variable prior to assigning a value to it. When you have those variable declarations at the file (class) level, they are assigned as soon as the script is first run/compiled. That's no good for you, because you don't know what the individual file urls should be until your program has already been running long enough to get the list. What you want is something more like this:

 import System.IO;
  
 var getPlaylist : String = "http://shuleii.dk/Guardian/getfiles.php";
 var urlBase : String = "http://shuleii.dk/Guardian/Playlist/";
  
 function Start () {
     var www : WWW = new WWW (getPlaylist);
     yield www;
     Debug.Log("Songs: " + www.text);
     var songNames : String[] =  www.text.Split(","[0]);

     for (var songUrl:String in songNames) {
         Debug.Log("One song url: " + urlBase + songUrl);
         // do something with the the song url here...
     }
 }

Also, if you have control over the PHP script, it might be easier to modify that to just return full URLs rather than filenames. Then you don't have to hardcode the extra URL in your unity code.

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

23 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Array Dump to String for Send via WWW 1 Answer

wwwform send an array the right way! 0 Answers

Get array from a php, put it into an array in Unity? 1 Answer

How to send securely scores to PHP backend 2 Answers

Disable execute/write permissions on PHP files while maintaining the ability to execute them from Unity? 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