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 unity_EoQvLrO5Uh1WDA · May 26, 2018 at 08:28 AM · null reference exceptionfilebrowserexplorer

how to solve null reference on file browser script

i'm new to Unity and C# and trying to make an game in which users could upload images and i'm using an free asset named Simple File Browser by Graces Games. it is an asset to browser files in the game, i'm trying to make use of it's Filebrowser class but not able to get any help.

here is my code

 using System.Collections;
 using UnityEngine.UI;
 using System.Collections.Generic;
 using UnityEngine;
 using System.Runtime.Serialization.Formatters.Binary;
 using System.IO;
 using System;
 
 namespace GracesGames.SimpleFileBrowser.Scripts
 {
     public class explorer_script : MonoBehaviour
     {
         private GameObject FileBrowserPrefab;
         public string[] FileExtensions = { "jpeg", "jpg", "png" };
         private GameObject _loadedImage;
         public RawImage image;
         public bool PortraitMode;
         void start()
         {
             
             PortraitMode = true;
         }
 
         public void OpenFileBrowser()
         {
             OpenFileBrowser(FileBrowserMode.Load);
         }
 
         private void OpenFileBrowser(FileBrowserMode fileBrowserMode)
         {
             FileBrowserPrefab = GameObject.Find("FileBrowser");
             // Create the file browser and name it
             GameObject fileBrowserObject = Instantiate(FileBrowserPrefab, transform);
             fileBrowserObject.name = "FileBrowser";
             // Set the mode to save or load
             FileBrowser fileBrowserScript = fileBrowserObject.GetComponent<FileBrowser>();
             fileBrowserScript.SetupFileBrowser(PortraitMode ? ViewMode.Portrait : ViewMode.Landscape);
  
             fileBrowserScript.OpenFilePanel(FileExtensions);
             fileBrowserScript.OnFileSelect += LoadFileUsingPath;
  
         }
 
         // Loads a file using a path
         private void LoadFileUsingPath(string path)
         {
             if (path.Length != 0)
             {
                 BinaryFormatter bFormatter = new BinaryFormatter();
                 // Open the file using the path
                 FileStream file = File.OpenRead(path);
                 // Convert the file from a byte array into a RawImage
                 RawImage fileData = bFormatter.Deserialize(file) as RawImage;
                 // We're done working with the file so we can close it
                 file.Close();
                 
             }
             else
             {
                 Debug.Log("Invalid path given");
             }
         }
 
     }
 }

so what i'm confused is that the FileBrowserPrefab requires an gameobject, how i'm supposed to attach prefab to it?

So what i do is that i create a gameObject add my script to it (which stated above), and then on the button onClick() i attach the gameObject (on which i have attached the script) and call the OpenFileBrowser() method?

but it's giving me following error..

NullReferenceException: Object reference not set to an instance of an object GracesGames.SimpleFileBrowser.Scripts.explorer_script.OpenFileBrowser (FileBrowserMode fileBrowserMode) (at Assets/explorer_script.cs:37) GracesGames.SimpleFileBrowser.Scripts.explorer_script.OpenFileBrowser () (at Assets/explorer_script.cs:26) UnityEngine.Events.InvokableCall.Invoke () (at C:/buildslave/unity/build/Runtime/Export/UnityEvent.cs:166) UnityEngine.Events.UnityEvent.Invoke () (at C:/buildslave/unity/build/Runtime/Export/UnityEvent_0.cs:58) UnityEngine.UI.Button.Press () (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/UI/Core/Button.cs:36) UnityEngine.UI.Button.OnPointerClick (UnityEngine.EventSystems.PointerEventData eventData) (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/UI/Core/Button.cs:45) UnityEngine.EventSystems.ExecuteEvents.Execute (IPointerClickHandler handler, UnityEngine.EventSystems.BaseEventData eventData) (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/EventSystem/ExecuteEvents.cs:50) UnityEngine.EventSystems.ExecuteEvents.Execute[IPointerClickHandler] (UnityEngine.GameObject target, UnityEngine.EventSystems.BaseEventData eventData, UnityEngine.EventSystems.EventFunction`1 functor) (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/EventSystem/ExecuteEvents.cs:261) UnityEngine.EventSystems.EventSystem:Update()

please any help will be really appreciated?

thanks..

Comment
Add comment · Show 8
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 Captain_Pineapple · May 26, 2018 at 09:37 AM 0
Share

Can you put some code like this:

 if(fileBrowserScript == null)
     {
         Debug.LogError("I forgot to add a FileBrowser-Component to my Object or I am not searching at the right Transform");
     }

after this line?

FileBrowser fileBrowserScript = fileBrowserObject.GetComponent<FileBrowser>();

If the error triggers you should know where to look.

avatar image unity_EoQvLrO5Uh1WDA Captain_Pineapple · May 26, 2018 at 10:49 AM 0
Share

hi! thanks for replying , so, yea i wrote that "if condition" , and it turns out that that script fileBrowserScript is indeed null, moreover, my second try was that i removed this line FileBrowserPrefab = GameObject.Find("FileBrowser"); but then i'm getting an error that the object you want to instantiate is null which i think is a legit error, but in the github they have a demoscript, and in that they haven't assigned any value to FileBrowserPrefab.

And when i check in the inspector of the Filebrowser object there is a empty field of FileBrowserPrefab.

so what should i do? assign a value explicitly as i have done in this line FileBrowserPrefab = GameObject.Find("FileBrowser"); or just let it be?

link to the demoscript thanks again for replying

avatar image Captain_Pineapple unity_EoQvLrO5Uh1WDA · May 26, 2018 at 12:58 PM 0
Share

Can you please post a screenshot of the inspector view of the Filebrowserprefab.

Show more comments
avatar image Hellium · May 26, 2018 at 11:08 AM 0
Share

Are you sure the gameObject named FileBrowser in your scene as a script called FileBrowser attached to it?

avatar image unity_EoQvLrO5Uh1WDA Hellium · May 26, 2018 at 11:23 AM 0
Share

No, on the object i have attached my own script , FileBrowser.cs is the one that came with the asset.

avatar image unity_EoQvLrO5Uh1WDA Hellium · May 26, 2018 at 11:31 AM 0
Share

alt text

so as you can see in the image i have attached my script (which is called Explorer_script, in which i want to use FileBrowser Script), and in the prefab i attached the asset of Filebrowser, but still no help.

prefab.png (125.3 kB)
avatar image unity_EoQvLrO5Uh1WDA Hellium · May 26, 2018 at 11:55 AM 0
Share

okay so, what I've done now is that I've created another object named FileBrowserSc and i have attached the FileBrowser.sc to it (which i got from the asset), and in another object FileBrowser and in it's FileBrowserPrefab is have done this FileBrowserPrefab = GameObject.Find("FileBrowserSc"); so now i have the object that has the script, but now i'm getting another error.

NullReferenceException: Object reference not set to an instance of an object GracesGames.SimpleFileBrowser.Scripts.FileBrowser.SetupFileBrowser (View$$anonymous$$ode newView$$anonymous$$ode, System.String startPath) (at Assets/GracesGames/SimpleFileBrowser/Scripts/FileBrowser.cs:102) GracesGames.SimpleFileBrowser.Scripts.explorer_script.OpenFileBrowser () (at Assets/scripts/explorer_script.cs:39) UnityEngine.Events.InvokableCall.Invoke () (at C:/buildslave/unity/build/Runtime/Export/UnityEvent.cs:166) UnityEngine.Events.UnityEvent.Invoke () (at C:/buildslave/unity/build/Runtime/Export/UnityEvent_0.cs:58) UnityEngine.UI.Button.Press () (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/UI/Core/Button.cs:36) UnityEngine.UI.Button.OnPointerClick (UnityEngine.EventSystems.PointerEventData eventData) (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/UI/Core/Button.cs:45) UnityEngine.EventSystems.ExecuteEvents.Execute (IPointerClickHandler handler, UnityEngine.EventSystems.BaseEventData eventData) (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/EventSystem/ExecuteEvents.cs:50) UnityEngine.EventSystems.ExecuteEvents.Execute[IPointerClickHandler] (UnityEngine.GameObject target, UnityEngine.EventSystems.BaseEventData eventData, UnityEngine.EventSystems.EventFunction`1 functor) (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/EventSystem/ExecuteEvents.cs:261) UnityEngine.EventSystems.EventSystem:Update()

any guesses?

1 Reply

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

Answer by GracesGames · May 26, 2018 at 12:47 PM

Answered here: https://github.com/GracesGames/SimpleFileBrowser/issues/39

Comment
Add comment · Show 8 · 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 unity_EoQvLrO5Uh1WDA · May 26, 2018 at 01:05 PM 0
Share

thanks i was just going to do that .

avatar image unity_EoQvLrO5Uh1WDA · May 26, 2018 at 03:44 PM 0
Share

hi, pardon me, but I've ran into another issue, the problem is that the explorer size is bigger than my canvas, and i know that you suggested about those methods in the Guthub issue but, the problem is that the objects are only made on runtime, and even if i scale them on runtime, if i play the game again the size goes bigger again, so is there any way that i can scale it dynamically like a canvas? something like scale with screen size so that the explorer fits to the screen size .

i'm sorry i know this is not related to the thread but i thought there might be easy answer to this and i being a rookie not able to find it.

thanks.

avatar image GracesGames · May 26, 2018 at 03:51 PM 0
Share

Hey,

As stated, you can change the values with the ways described in the GitHub issue. Another option is to drag an instance of the UI prefab on the canvas in the Hierarchy and edit the values of the prefab. When you are content with the result save the values to the prefab by pressing Apply in the inspector. The newly created UI will then use these values.

Hope that helps.

avatar image unity_EoQvLrO5Uh1WDA GracesGames · May 26, 2018 at 09:59 PM 0
Share

i'm sorry but i'm not able to get what you are trying to say, the FileBrowserUI gets created in the runtime and i need to udjust it's Rect Transform to get it working (adjust it's scale and position)... how am i supposed to drag it? or is there any other thing i need to grab??

avatar image GracesGames unity_EoQvLrO5Uh1WDA · May 27, 2018 at 10:37 AM 1
Share

Only an instance of the File Browser UI prefab is created at run-time. If you change the prefab in the editor, and apply them to the prefab, the updated values are used. To create in instance of the prefab for editing just drag it to the hierarchy. For the UI, you should drag it onto the Canvas object.

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

85 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 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 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 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

Hide hidden directories in file browser. 1 Answer

Null Reference Exception Help 1 Answer

NullReferenceException: Object reference not set to an instance of an object? 1 Answer

im getting this error: NullReferenceException: Object reference not set to an instance of an object Item.Start () (at Assets/Scripts/Inventory System/Item.js:9) 1 Answer

I Can't find the ZomBear Avatar! 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