- Home /
Trying to refer to my Scripts, not working...?
Hey, I have a working script that dissables MouseLook, CharacterMovement etc. And displays in the screen Menu, and Respawn. The thing is that i did not write it entirely and I was playing around thinking that i can improve it and disable more actions from the player (because in the respawn menu i can still turn a flashlight on and of (flashlight is a simple light in the MainCamera), and my HideMouseCursor it's still hided, when it should be visible to click "Menu" or "Respawn". Well the problem is that i tried to disable this scripts in the same way the other scripts where disabled, and it doesn't work in that way..., the Console shows this error :
NullReferenceException: Object reference not set to an instance of an object RespawnMenu.Update () (at Assets/Scripts Mios/RespawnMenu.js:30)
This is the Script, the Bold lines are the lines that i writed, and are not working...(PD: Sorry for my English haha):
var lookAround01 : MouseLook;
var lookAround02 : MouseLook;
var charMotor : CharacterMotor;
var charMotor2 : FPSWalkerEnhaced;
**var HideCursor : HideMouseCursor;
var FlashlightHide : FlashLight;**
static var playerIsDead = false;
function Start ()
{
lookAround01 = gameObject.GetComponent(MouseLook);
lookAround02 = GameObject.Find("MainCamera").GetComponent(MouseLook);
charMotor = gameObject.GetComponent(CharacterMotor);
charMotor2 = gameObject.GetComponent(FPSWalkerEnhaced);
**HideCursor = gameObject.GetComponent(HideMouseCursor);
FlashlightHide = gameObject.GetComponent(FlashLight);**
}
function Update ()
{
if (playerIsDead == true)
{
lookAround01.enabled = false;
lookAround02.enabled = false;
charMotor.enabled = false;
charMotor2.enabled =false;
**HideCursor.enabled =false;
FlashlightHide.enabled =false;**
}
}
function OnGUI ()
{
if (playerIsDead == true)
{
if (GUI.Button(Rect(Screen.width*0.5-50, 200-20, 100, 40), "Respawn"))
{
RespawnPlayer();
}
if (GUI.Button(Rect(Screen.width*0.5-50, 240, 100, 40), "Menu"))
{
Debug.Log("Return to Menu");
}
}
}
function RespawnPlayer ()
{
Debug.Log("Respawn Player");
}
@script RequireComponent(CharacterController)
Sorry, the Bold lines didn't show as I expected -->(**)
Answer by SilentSin · Oct 23, 2013 at 12:41 AM
The obvious answer is that the game object doesn't actually have those scripts on it (or one is missing).
call Debug.Log(HideCursor); and the same for FlashlightHide immediately after you assign them to see if they're null or not.
I think you are right, but the "Hide$$anonymous$$ouseCursor" Script is attached to the "Player" itself and the "FlashLight" Script to a normal Spotlight wich is attached to the "$$anonymous$$ainCamera"... As i'm a bit new on this I don't know how I can refer to this objects in particular in the script, how should i do it??, Can you help me?
Answer by fernandovt · Oct 23, 2013 at 01:46 AM
Ok I found the solution to the flashlight here is what i did:)
1) var FlashlightHide : FlashLight; 2) FlashlightHide = GameObject.Find("FlashLight").GetComponent(FlashLight); 3) FlashlightHide.enabled =false;
The HideCursor thing didn't work, but i'm happy with the flashlight at least:)
The HideMouseCursor Script is attached to the player, i tried this and it still doesn't work:
1) var HideCursor : HideMouseCursor;
2) HideCursor = GameObject.Find("Player").GetComponent(HideMouseCursor);
3) HideCursor.enabled =false;
PD: The cursor Script:
function Update () { Screen.showCursor = false; }
Your answer
Follow this Question
Related Questions
How could I have a ball chooser menu? 1 Answer
Finding the player that walked into a ray? 1 Answer
A do nothing script? 2 Answers
add lockcursor to pause menu script?? 2 Answers
Respawn Problem 2 Answers