I’m new to scripting and Unity and I’m trying to make a little game. I want the player to start with no flashlight, and eventually pickup a flashlight that is in the environment. I’ve got the flashlight to work, and it is currently attached to the player, and can be turned on/off with the left mouse. I don’t know how to make a flashlight able to be picked up and how to start the player off without it. I’ve seen questions similar to this, but none had a definite answer or confused me too much. Can anyone help me out?
The flashlight object in the world has to have a collider component attached to it, you can use the function OnCollisionEnter in a script attached to the flashlight to check if there was a collision between the player and the flashlight, if so then turn on the players flashlight.
function OnCollisionEnter(collision : Collision) {
if (collision.gameObject.tag == "Player1")
{
//access player properties script, you need to attach a script on the player
var playerProperties : ScriptOnPlayer = collision.gameObject.GetComponent(ScriptOnPlayer);
playerProperties.ActivateFlashlight(true);
}
}
then the script attached to the player called ‘ScriptOnPlayer’ for example would have :
function ActivateFlashlight(newValue : boolean){
flashlightOn = newValue;
}