Scripting problem (120981)

Hello guys! I have a question - what’s wrong in my code?

I have 2 scripts, one to trigger function and one that is triggered.

First one is a child null object with script

#pragma strict
var TheSystem : Transform;

function Update ()
{
	if (Input.GetKeyDown(KeyCode.E))
	{
		TheSystem.SendMessage("SwitchIt",SendMessageOptions.DontRequireReceiver);
	}
}

// TheSystem - is Player, it's dragged in box TheSystem properly

Second one is on a lightswitch, that must be switched

#pragma strict

var SwitchIsUp = true;

function SwitchIt ()
{
		if (SwitchIsUp == true)
		{
		gameObject.animation.Play("SwitchDown");
		SwitchIsUp = false;
		}
		else
		{
		gameObject.animation.Play("SwitchUp");
		SwitchIsUp = true;
		}
}

The problem is - nothing happens, and I can’t understand why. There’s no errors niether. I’m a begginer in scripting tbh.

Thank you)

Added:

Btw, if there’s only one script

#pragma strict

var SwitchIsUp = true;

function Update ()
{
	if (Input.GetKeyDown(KeyCode.E))
	{
		if (SwitchIsUp == true)
		{
		gameObject.animation.Play("SwitchDown");
		SwitchIsUp = false;
		}
		else
		{
		gameObject.animation.Play("SwitchUp");
		SwitchIsUp = true;
		}
	}
}

Animations are playing. :slight_smile:

It's funny, I've tried thing with debuglog, In one piece of code - it's working, separatly - not #pragma strict function Update () { if (Input.GetKeyDown(KeyCode.E)) { SendMessage("SwitchIt", SendMessageOptions.DontRequireReceiver); } } function SwitchIt () { Debug.Log("WrongFunction"); } Separated part , that includes only function SwitchIt(){..} cuted and pasted , as i understand, can be attached to any object in the scene, but still the result is negative to me (

–

2 Answers

2

hello, i dont use javascript but a few points,

is this script on a gameobject in the scene?

try using on keyup because on key down could be getting called multiple time before you release the key as the update function loops fast.

also try to get it working in one script before splitting them so you can locate the problem easier.

[34582-bandicam+2014-10-31+16-01-26-742.jpg|34582] [34583-bandicam+2014-10-31+16-01-38-743.jpg|34583] @SteveBC Here's pictures of how I attached scripts. @james_170482 thanks, I'll try it out, but still, with an Update script of getkey in switch script - it's working, i think the problem is in calling function when i try to separate them out. But as I learned, the sendmessage line is correct, and the names of functions too, so i'm stuck and feeling myself like "it's not working cuz it's not your thing" :|

–

This question may be cosidered as answered) Thx a lot for your help! The problem was in import, I solved it with prefabing the object needed to be triggered and repositioning it into my scene. Thanks again, and sorry if my eng is not good enought ^^

–

Your scripts work fine for me, so the problem seems to be with how your objects are set up in your scene.
Did you perhaps forget to assign the GameObject to your TheSystem variable in the Inspector?