- Home /
Can't access a javascript static variable from c# script
Hello! I'm trying to modify a static var contained in a javascript script from a C# script - given to the same gameobject - but i get an error. The problem seem to be the syntax. This is my C# code:
using UnityEngine;
using System.Collections;
public class NewBehaviourScript : MonoBehaviour {
public void SetCheckBoxValue(string myCheckBoxVal)
{
if (myCheckBoxVal == "true"){
gameObject.GetComponent("GameControllerScript").gameMode = 1;
}
if (myCheckBoxVal == "false"){
gameObject.GetComponent("GameControllerScript").gameMode = 2;
}
}
}
The error i get is "Type "UnityEngine.Component" does not contain a definition for "gameMode" and no extension method "gameMode" of type "UnityEngine.Component" could be found (are you missing a using directive or an assembly reference?)"
Answer by Itinerant · Jan 30, 2013 at 03:51 PM
Go check out Unity Gems, about halfway through their list of 'Unity Gotchas.' http://unitygems.com/mistakes1/
Is the script you're calling in the plugins, standard assets, or pro standard assets folders? C# compiles before javascript, so it can't refer back, unless you put the js in one of those folders, which will force the program to compile it first.
I already knew that, but even giving a custom script execution order or putting the script that needs to be compiled first into plugins folder doesn't solve the problem: i still get the same error.
I$$anonymous$$O it's best not to mix JS and CS. If I were you I'd convert the JS to CS and trash the JS version.
Try taking out the "" from the getcomponents. I'm not really sure why, but I've had that solve a number of problems.
Your answer
Follow this Question
Related Questions
how to acess Static variable in other scripts without extended functions? 2 Answers
Access a JavaScript variable from a C# script? 2 Answers
Need to use 2 different language scripts. 1 Answer
Lower player's health from separate script. 2 Answers
Trivial question for Javascript/Unityscript experts: static variables? 4 Answers