- Home /
Making A Script for my Switch
I have a really good script for switchs, that includes the light that can be turned on, and of, and a sound. The problem with it is that no matter where I click with the mouse, the switch gets activated... Anyone could help me with this problem?, here is my script:
var linkedLight : Light; var shootSound:AudioClip;
function Update () { if(Input.GetKeyDown(KeyCode.Mouse0)){ linkedLight.enabled = !linkedLight.enabled; audio.PlayOneShot(shootSound); } }
Some more information are needed here. You could tell us what is the switch, is it a game object somewhere in the scene, is it a GUI button? If it is a GO, does it have a collider? From there on, someone else will help you as I gotta go...
It's an object, a switch model I downloaded, with no animation sadly. It has a tag called "Switch", and a box collider attached to it:)
Answer by Anusha · Sep 05, 2012 at 06:29 AM
i am assuming your switch is an object in the scene and its been tagged "switch"... then on mouse click you need to do raycast and chech whether you hit the switch...if yes then continue. im going to give c# script... but you get the idea...
function Update ()
{
if(Input.GetKeyDown(KeyCode.Mouse0))
{
var ray = Camera.main.ScreenPointToRay (Input.mousePosition);
var hit : RaycastHit;
if (Physics.Raycast (ray, hit, 100))
{
if(hit.transform.tag == "switch")
{
linkedLight.enabled = !linkedLight.enabled;
audio.PlayOneShot(shootSound);
}
}
}
}
It's an object, a switch model I downloaded, with no animation sadly. It has a tag called "Switch", and a box collider attached to it:) Your script should work maybe, but it has some errors, and as I'm not a scripting guy, I have tried changing it too long and it still has. The game doesn't start because it tells me "all compiled errors has to be fixed before you can enter play mode" I think there are a few errors, they appar on this order unexpected token: public insert semicolon at the end insert semicolon at the end expected }, found public insert a semicolon at the end expecting ), found "hit" expecting ), found "," unexpected token: 100 unexpected token: if insert semicolon at the end expecting:, found "="
I also noticed this script is in C#. He's scripting in .js
please check now...the code is in js now.... hope it works...
Your answer
Follow this Question
Related Questions
Zombie Round Script Help 1 Answer
Spawn Script Not Working 1 Answer
[SOLVED] Problem with "foreach". 1 Answer
Bullet not moving 1 Answer
mistake in script 1 Answer