Stuck with GetComponent

Hey there,

I’m trying to play with 2 differrent script with GetComponent but it’s really confusing …

This is what i wrote

MoneyConsomable.js

var inv = GameObject.FindGameObjectsWithTag("Inventory");

var Valor : int;
var Effect : GameObject;
var pickUp : AudioClip;




function OnCollisionEnter ( myCol: Collision ) {
  
  if (myCol.gameObject.tag == "Player"   inv.gameObject.GetComponent(Money).curMoney < inv.gameObject.GetComponent(Money).maxMoney) 
  
  {
  inv.gameObject.GetComponent(Money).curMoney += Valor;
  Debug.Log("You got :"+ Valor +"$");
  
  Destroy(this.gameObject);
  
  Instantiate(Effect, transform.position + Vector3(0,5,0), Quaternion.identity);
  audio.PlayOneShot(pickUp);
  
  
  
  }
  
}

Money is attached in “Inventory” gameObject
MoneyConsomable is attached to the consomable

I don’t know why it don’t work

I also tried with this doc : Unity - Scripting API: Component.GetComponent
but it’s really hard to understand

Sorry for my english i’m french

Edit : Console

I got these errors :

UnityException: You are not allowed to call this function when declaring a variable.
Move it to the line after without a variable declaration.
If you are using C# don’t use this function in the constructor or field initializers, Instead move initialization to the Awake or Start function.
MoneyConsomable…ctor () (at Assets/Script/Consomable/MoneyConsomable.js:1)

FindGameObjectWithTag can only be called from the main thread.
Constructors and field initializers will be executed from the loading thread when loading a scene.
Don’t use this function in the constructor or field initializers, instead move initialization code to the Awake or Start function.

Try this:

var inv : GameObject;
var Valor : int;
var Effect : GameObject;
var pickUp : AudioClip;

function Awake() {
    inv = GameObject.FindGameObjectsWithTag("Inventory")[0];
}

function OnCollisionEnter ( myCol: Collision ) {
  if (myCol.gameObject.tag == "Player"   inv.gameObject.GetComponent(Money).curMoney <    inv.gameObject.GetComponent(Money).maxMoney) 
  {
  inv.gameObject.GetComponent(Money).curMoney += Valor;
  Debug.Log("You got :"+ Valor +"$");
  Destroy(this.gameObject);
  Instantiate(Effect, transform.position + Vector3(0,5,0), Quaternion.identity);
  audio.PlayOneShot(pickUp);
  }
}

You can also use the Start() method

var inv : GameObject;
var Valor : int;
var Effect : GameObject;
var pickUp : AudioClip;

function Start() {
    inv = GameObject.FindGameObjectsWithTag("Inventory")[0];
}

function OnCollisionEnter ( myCol: Collision ) {
  if (myCol.gameObject.tag == "Player"   inv.gameObject.GetComponent(Money).curMoney <    inv.gameObject.GetComponent(Money).maxMoney) 
  {
  inv.gameObject.GetComponent(Money).curMoney += Valor;
  Debug.Log("You got :"+ Valor +"$");
  Destroy(this.gameObject);
  Instantiate(Effect, transform.position + Vector3(0,5,0), Quaternion.identity);
  audio.PlayOneShot(pickUp);
  }
}

Updated my first post.

Thanks for your help !! Worked :slight_smile:

I used that :

function Awake ()

{

inv =  GameObject.Find("Inventory");

}

Awesome! :slight_smile: