- Home /
 
Getcomponent Javascript error
Hello so im new to java script, and get this error No appropriate version of 'UnityEngine.Component.GetComponent' for the argument list '(function(): void)' was found.
im trying to get the "money" var from the the Main script to use in my Store script.
Here is my codes:
First one:(called Main)
 #pragma strict
 var clicks : GameObject;
 var totalclicks : GUIText;
 var moneytext : GUIText;
 var clicked : boolean = false;
 var click : int = 0;
 public static var money : int = 0;
 function Start () {
 click = 0;
 money = 0;
 
 }
 
 function Update () {
 totalclicks.text = "Total Weeds: " + click;
 moneytext.text = "Weed Money: " + money;
 
 }
 
 function OnMouseDown()
 {
 click += 1;
 money += 1;
 }
 
 ------------------------
 Second one(called Store):
 #pragma strict
 var factoryt : GUIText;
 var factory : int = 0;
 
 private var mainScript : Main;
 
 function Awake (){
 mainScript = GetComponent(Main);
 
 }
 
 function Start () {
 factory = 0;
 }
 
 function Update () {
 factoryt.text = "Factory: " + factory;
 }
 
 function OnMouseDown(){
 if(mainScript.money >= 100){
 factory += 1;
 mainScript.money -= 100;
 }
 
 }
 
               Answer by Landern · Mar 20, 2015 at 01:09 PM
Ensure that the Store script is on the same object as the Main script otherwise it will not find it. GetComponent is not a global find X anywhere. That said, ensure you use the right GetComponent function/method.
 function Awake (){
     mainScript = GetComponent.<Main>(); // use the generic version to get it in java/unityscript
 }
 
              Answer by tanoshimi · Mar 20, 2015 at 01:10 PM
"Main" is a reserved keyword in many programming languages. Call your script something else ("MainScript " would be fine) and also change the GetComponent call to match.
Answer by YoungDeveloper · Mar 20, 2015 at 01:11 PM
Try changing you class "Main" name to something else.
Your answer