Can you access a C# var with Javascript?

My apologies is this appears to be a stupid question but I have searched the forums for an answer but haven’t found one :frowning:

I have an object with a C# script running on it for following a moving object around the screen. However there are times when I wish to deactivate this script for a certain amount of time and then reactivate it, all during run time.

I have a variable in the C# script that if set to “0” all its actions are deactivated and while set to “1”, run all actions associated with the script.

The problem is, how to a access and change this variable in runtime, and can I use a .js to do it?

Ive seen all the stuff on global vars in .js and understand how it works but I guess the issue is that I’m trying to cross communicate with two langs…

:cry: :cry:

Just abit more info, while my scene is running and I just change that Script Run var to 0 it stops like I want it to, and takes off again when set back to 1.

I’m still searching the forum and funnily finding almost answers using my own topic title as the search param but I’m still a little confused…

Ive moved the C# script to different folders from the JS ones because apparently that’s an issue but still having a read and write problem FRAK!

Any help appreciated :slight_smile:

171598--6173--$vars_212.jpg

a different folder won’t suffice.
The folder you put it in must be a folder thats compiled earlier.
The documentation contain informations about the compilation order in the advanced section.

Basically you would put this in the Standard Assets or plugins folder and the js script that accesses it in a folder different from the standard or pro asset or plugin folder

Thank you, I have moved the scripts.

Also I found a similar post about it but I’m new to unity (obviously) and I don’t really understand the syntax of it:

var javaScriptVariable : C#Object = GameObjectWithC#Atached.GetComponent(C#Object);

Im my mind

C#Object = ?? I don’t know…

GameObjectWithC#Atached = MyShipModel - With the attached C# Script

GetComponent(C#Object); = Access the Script attached to the MyShipModel

Even if I got that far, what further step do I need to access the C# variable called ScriptRun and modify its value?

Cheers

I would think SendMessage is enough as both components seem to be in the same GameObject and no reference is required.

C#

using UnityEngine;
using System.Collections;

public class Rotator : MonoBehaviour
{
	bool rotating = true;
	
	void Update ()
	{
		if (rotating)
			transform.Rotate(Vector3.up * Time.deltaTime * 10, Space.World);
	}
	
	void SetRotating(bool act)
	{
		rotating = act;
	}
}

Javascript:

var toggle = true;
function OnMouseDown ()
{
	toggle = !toggle;
	SendMessage("SetRotating", toggle);
	Debug.Log("SetRotating " + toggle);
}

Add both scripts to a cube in scene and set collider to trigger.

More here:

Hi Tri3,

Ive tried that but nothing happens, I assume that when the bool is false the cube should stop rotating?

I keeps rotating no matter what value the toggle is set to?

Now if I try and use similar code like:

var SetScriptRun = 0;

function Update ()
{
   SendMessage("ScriptRun", SetScriptRun);
   Debug.Log("ScriptRun" + SetScriptRun);
}

Again nothing happens and I get this message in the console:

SendMessage ScriptRun has no receiver!
UnityEngine.Component:SendMessage(String, Object, SendMessageOptions)

… this is so frustrating :frowning:

Hi, you should not put SendMessages into your update loop, this will cause many and often.

look here : Unity - Scripting API: SendMessageOptions.RequireReceiver

Maybe there is no recieving function available on your gameObject ?


oxl

Well yes that probably correct hence the error message, and the only reason its in the Update is for testing.

However I am no closer to a solution, what kind of a C# function do I have to write to receive the SentMessage?

…This is just so weird, I’m used to working in DxStudio (JavaScript) Flash (ActionScript), where referencing any variable across all GameObjects is very very easy…

It’s just like a normal void myFunction(whatever), but the recieving script has to be attached to the same GameObject.

For accessing other GO’s through references, please take a look here :
http://unity3d.com/support/documentation/ScriptReference/index.Accessing_Other_Game_Objects.html
and here
Team Messenger & Online Collaboration Platform – Flock .

Do a query for “accessing game object”. There will be plenty of posts.

Btw, it is not the perfect solution to mix JS and C# scripts alot (see Dreamoras post), I would pick one and stay with it.


oxl

Thank you, Ive finally figured it out woohoo!!

Just using code like this for testing but this is working perfectly;

var SetScriptRun = 0;

function Update ()
{
	SetScriptRun += 1;
	
	if( SetScriptRun < 200){
	GameObject.Find("Ship").GetComponent(typeof(SeekSteer)).ScriptRun = 0;
	}
	
	if( SetScriptRun > 200){
	GameObject.Find("Ship").GetComponent(typeof(SeekSteer)).ScriptRun = 1;
	}
	if( SetScriptRun > 600){
	SetScriptRun = 0;
	}
	print(SetScriptRun);
}

I see what is happening. You try to set the field value directly. Using SendMessage you need to add a method to be the receiver. In this case:

Javascript:
   SendMessage("HandleScriptRun", SetScriptRun);

C#:
   void HandleScriptRun(value)
   {
      ScriptRun = value
   }

edit: But that GetComponent works just as fine! :smile:

What you are suggesting looks far more simplified than my current method, but when I put that code in the C# script it gives me an error?

void HandleScriptRun(value){ : It says Identifier expected?

Sounds like there is something silly I’m missing here, but C# is brand new to me :confused:

Two years too late, but I think its telling you that you did not identify your new variable “ScriptRun”. C is more strict with new variable declarations than JS. And yes, JS may have created the variable, but C doesnt know that. She can grab the value, but not the variable name from JS. Declare ScriptRun as a new C integer.

Change:

ScriptRun = value

To:

int ScriptRun = value;

and see if that works.