Player UI prefab slider Null reference
I have a Multiplayer game Player UI which is instantiated on start. However a Slider named and Tagged as Throttle under the Prefab throws a Null reference when being called under start function when using;
Throttle = GameObject.Find ("Throttle").GetComponent ();
Thanks.
Will share the code on a kind response.
Answer by Jawchewa · May 30, 2017 at 04:33 AM
When using get component, you have to specify the type of the component that you are trying to get. I think what you want should look more like this:
Slider Throttle = GameObject.Find("Throttle").GetComponent<Slider>();
This will get you the slider component, assuming that the object named "Throttle" exists at the point when you call this, and has a slider component attached.
Answer by mohitsabhi · May 30, 2017 at 05:23 AM
@Jawchewa Thanks very much for your kind response. Tried but think I am still doing something wrong, coz still giving a Null Reference.
Sharing in scripts and the screen for the scene;
a. first is the Player Controller Script which has the Throttle Slider script and the lower part has Player Setup script which is used to Instantiate the PlayerUI Prefab under which the Throttle slider resides.
b. Screen shot of the Prefabs folder showing the hierarchy of the Player Prefab and the right-side is the PLAYER Inspector.
Thanks for your time and await yours.
Brgds
Hmm. $$anonymous$$y guess is that the start function in your ship movement script is probably getting called before the start function of your player setup script, and so at the time when you want to get the throttle, it probably doesn't exist in the scene yet. Try changing your player setup script by replacing the Start method with Unity's built in Awake method to force it to be called before your ship movement's Start method.
Thanks so much. Tried it like this;
void Start () { if (!isLocalPlayer) { DisableComponents (); AssignRemoteLayer (); } else { sceneCamera = Camera.main; if (sceneCamera != null) { sceneCamera.gameObject.SetActive (false); }
}
GetComponent<Player> ().Setup ();
} void Awake ()
// CREATE PLAYER UI AND CONTROLS
{
playerUIInstance = Instantiate (playerUIPrefab);
playerUIInstance.name = playerUIPrefab.name;
}
This sorted for the Local player, however when the Client joins in now see Two PlayerUIs created on the Heirarchy window and on the Network Player canvas, screen shot attached.
How do I ensure the same Player UI used ins$$anonymous$$d of duplication.
Thanks once again.
Brgds
Am unable to attach the Screen shot but its showing TWO PlayerUIs on the Heirarchy window and on the Client canvas.
Answer by alirazabrame · Oct 08, 2021 at 08:05 PM
Hi! I have the same problem. I want to load loading screens on button click. I always got null pointer exception.
I make changes to resolve my problem:
Step-1: Add my all canvases in an empty object
Step-2: Assign all canvases same tag
Step-3: Inactive all canvases
Step-4: Active the required canvas on button click
Step-5: Atlast activate the script function
May be your problem different. But I suggest you to Instantiate prefab before calling the script function.
Your answer
![](https://koobas.hobune.stream/wayback/20220612123056im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Prefab UI Slider misses the fill and background items... 0 Answers
Unity3d Script does not work anymore 1 Answer
I need to know how to spawn a prefab and then have it propelled forward as if thrown 0 Answers
Spawned Rocket(Clones) still here after scene swap. 1 Answer
scripting SetActive(true_false)? 1 Answer