- Home /
Help| Flashlight sync' over network
I'm trying to sync the flash light , That other people will be able to see the light
This is the error: Assets/Resources/OldScripts/Misc/FlashLight.js(22,9): BCE0004: Ambiguous reference 'light': FlashLight.light, UnityEngine.Component.light.
This is a code I found: var light : Light; private var lightOn : boolean = false;
function Update () {
if(networkView.isMine){
if(Input.GetKeyDown(KeyCode.F)){
if(!lightOn){
networkView.RPC("TurnLightOn", RPCMode.All, true); //Turn on
}
else{
networkView.RPC("TurnLightOn", RPCMode.All, true); //Turn off
}
lightOn = !lightOn;
}
}
}
@RPC
function TurnLightOn (on : boolean) {
lightOn = on;
if(on){
light.intensity = 1;
}
else{
light.intensity = 0;
}
}
ALSO I WANT TO KNOW: HOW TO SYNC THE FLARE OF THE LIGHT? I MEAN, HOW TO MAKE PEOPLE TO GET Blinded FROM THE FLARE OF MY FLASHLIGHT WHEN THERE'RE LOOKING AT ME. THANKS !
So what is the question regarding syncing the flashlight? You have code to do it already.
I forgot to post the error: Assets/Resources/OldScripts/$$anonymous$$isc/FlashLight.js(22,9): BCE0004: Ambiguous reference 'light': FlashLight.light, UnityEngine.Component.light.
You created a variable light. But light
already refers to the light component added to the object. Thats why you get that error. Rename your variable!
This is the new script:
var lightt : Light;
private var lightOn : boolean = false;
function Update () {
if(networkView.is$$anonymous$$ine){
if(Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.F)){
if(!lightOn){
networkView.RPC("TurnLightOn", RPC$$anonymous$$ode.All, true); //Turn on
}
else{
networkView.RPC("TurnLightOn", RPC$$anonymous$$ode.All, false); //Turn off
}
lightOn = !lightOn;
}
}
}
@RPC
function TurnLightOn (on : boolean) {
lightOn = on;
if(on){
lightt.light.intensity = 1;
}
else{
lightt.light.intensity = 0;
}
}
No errors :) But now even I can't see the light, cause he's not turnning on\off :\ It's not working
It doesn't work because you are also caling the RPC
on yourself.
light is on
When the key is pressed, an RPC is sent to ALL.
The RPC is "recieved" and sets light off
light is toggled back on
light is on
As you can se, simply following the flow of your program solves the problem :)
This process is called "desk Checking". You simply run through your program as if you were the computer.
As a last thing. DO NOT post questions in answers. If it doesn't fix your problem but it is part of the question, edit the question or post a comment. Answers should always answer the question
Answer by jeuxbastien3311 · Jul 23, 2013 at 01:31 PM
public var thisLight : Light;
function Start()
{
thisLight = GetComponent("Light");
}
function Update()
{
if(Input.GetKeyDown("f") && networkView.isMine)
{
if(thisLight.enabled)
{
networkView.RPC("DisableLight", RPCMode.AllBuffered);
}
else
{
networkView.RPC("EnableLight", RPCMode.AllBuffered);
}
}
}
@RPC
function EnableLight()
{
thisLight.enabled = true;
}
@RPC
function DisableLight()
{
thisLight.enabled = false;
}
$$anonymous$$ind adding some explanation to your answer? :)
PS: You will have problems using this exact method, due to RPC Buffering
I have a problem with a script I want to do the same thing but with this script
can you help me please :)
//Name this script Flashlight and attach it to your player for instance
var lightSource : Light; //Connect the light source in the Inspector
static var energy : float = 100; //The energy amount of the flashlight
static var turnedOn : boolean = false; //Boolean to check whether it's turned on or off
private var drainSpeed : float = 2.0; //The speed that the energy is drained
function Update () {
if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.F)) ToggleFlashlight();
}
//When the player press F we toggle the flashlight on and off
function ToggleFlashlight () {
turnedOn=!turnedOn;
if (turnedOn && energy>0) {
TurnOnAndDrainEnergy();
} else {
lightSource.enabled = false;
}
}
//When the flashlight is turned on we enter a while loop which drains the energy
function TurnOnAndDrainEnergy () {
lightSource.enabled = true;
while (turnedOn && energy>0) {
energy -= drainSpeed*Time.deltaTime;
yield;
}
lightSource.enabled = false;
}
//This is called from outside the script to alter the amount of energy
static function AlterEnergy (amount : int) {
energy = $$anonymous$$athf.Clamp(energy+amount, 0, 100);
}
@jeuxbastein3311 How about you ask a question, ins$$anonymous$$d of posting a comment? No one will answer you here