- Home /
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..
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.
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
Can you please post a screenshot of the inspector view of the Filebrowserprefab.
Are you sure the gameObject named FileBrowser
in your scene as a script called FileBrowser
attached to it?
No, on the object i have attached my own script , FileBrowser.cs is the one that came with the asset.
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.
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?
Answer by GracesGames · May 26, 2018 at 12:47 PM
Answered here: https://github.com/GracesGames/SimpleFileBrowser/issues/39
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.
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.
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??
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.