Hey there!
Recently I just wanted to build a game I have programmed in unity 5.
When I play the game in unity play-mode it runs flawless, but unity throws loads of errors yet, which are always the following two, every time regarding another game object:
#1
GetComponentFastPath 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.
#2
ArgumentException: GetComponentFastPath 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.
UnityEngine.Component.GetComponent[AudioSource] () (at C:/buildslave/unity/build/artifacts/generated/common/runtime/UnityEngineComponent.gen.cs:45)
ScoreUp - Kunai..ctor () (at Assets/ScoreUp - Kunai.js:4)
Well and that’s the ScoreUp - Kunai script:
#pragma strict
var kunai:int = 10;
var sound1: AudioSource = GetComponent.<AudioSource>();
function Start () {
}
function OnCollisionEnter (col : Collision) {
Destroy(this.gameObject);
sound1.Play();
GetComponent(ScoreScript).score += kunai;
}
As the error says, I tried to put the GetComponent thing into the Start function, like that:
#pragma strict
var kunai:int = 10;
var sound1: AudioSource;
function Start () {
sound1 = GetComponent.<AudioSource>();
}
function OnCollisionEnter (col : Collision) {
Destroy(this.gameObject);
sound1.Play();
GetComponent(ScoreScript).score += kunai;
}
After I did this all the errors were gone, BUT from this moment on there has been no sound playing when the item gets collected and the score has not been counting as well.
I don’t have any clue what to do to solve this^^
please help anyone…