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.