Argument Exception: GetComponentFastPath? built .exe of the game crashes!
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...
you're Destroying the gameObject before the sound could Play()
function OnCollisionEnter (col : Collision) {
sound1.Play();
GetComponent(ScoreScript).score += kunai;
Destroy(this.gameObject);
}
thanks for your answer, but after I tried your advice, the game object doesn't even disappear anymore... and the other problems still remain :(
this works perfectly here :
#pragma strict
var sound1: AudioSource;
function Start () {
sound1 = GetComponent.<AudioSource>();
}
function OnCollisionEnter (col : Collision) {
sound1.Play();
GameObject.FindObjectOfType(Score).score += 10;
Destroy(this.gameObject);
}
Score.js
#pragma strict
var score : int;
function OnGUI(){
GUILayout.Label(score.ToString());
}
Does the Collision happens?
function OnCollisionEnter (col : Collision) {
Debug.Log("Collision Enter");
sound1.Play();
GetComponent(ScoreScript).score += kunai;
Destroy(this.gameObject);
}
yeah the collision happens.
but the object only gets destroyed, if I have the code like that:
function OnCollisionEnter (col : Collision) {
Debug.Log("Collision Enter");
Destroy(this.gameObject);
sound1.Play();
GetComponent(ScoreScript).score += kunai;
}
I'm sorry to hear that. At the moment I'm not around a Computer. I'll create a test scene once I got home. In the next couple of hours. Have you tried hexagonius solution? eg.:
Destroy(this.gameObject, sound1.Length);
Answer by hexagonius · Sep 07, 2015 at 08:35 PM
use the Destroy with the second parameter for a delay. put in the audio clip length. on collection, disable the collider and renderer. this way the object will look like being collected but stays alive long enough to play the sound.
I have also tried this, but it didn't work either....
What I just don' t understand is that it has worked fine as I had the code like that:
#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;
}
and after putting the initialisation of the audio source into the awake function, anything is working at all :S
Answer by oliver-jones · Nov 16, 2015 at 08:09 PM
Your issue is because you're calling a GetComponent outside a function.
var sound1: AudioSource = GetComponent.<AudioSource>();
This is whats throwing you an error. You want to do it like this:
var sound1: AudioSource;
function Awake(){
sound1 = GetComponent.<AudioSource>();
}
Answer by aon9426 · Oct 26, 2016 at 06:53 AM
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.
my code
var mov :MovieTexture = GetComponent.().material.mainTexture;
mov.Play(); mov.loop = true;
Your answer
Follow this Question
Related Questions
Problem to read an array from another script but on the same object 1 Answer
Audio Endless Loop c# 1 Answer
flashlight on and off 1 Answer