Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
1
Question by Jan182 · Sep 07, 2015 at 02:28 PM · javascriptgetcomponentaudiosourcepathfast

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...

Comment
Add comment · Show 9
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Positive7 · Sep 07, 2015 at 03:06 PM 0
Share

you're Destroying the gameObject before the sound could Play()

 function OnCollisionEnter (col : Collision) {
 sound1.Play();
 GetComponent(ScoreScript).score += kunai;
 Destroy(this.gameObject);
 }
avatar image Jan182 · Sep 07, 2015 at 06:25 PM 0
Share

thanks for your answer, but after I tried your advice, the game object doesn't even disappear anymore... and the other problems still remain :(

avatar image Positive7 · Sep 07, 2015 at 06:54 PM 0
Share

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());
 }
 

avatar image Jan182 · Sep 07, 2015 at 07:11 PM 0
Share

I'm sorry, but the problems remain as before :S

avatar image Positive7 · Sep 07, 2015 at 07:30 PM 0
Share

Does the Collision happens?

 function OnCollisionEnter (col : Collision) {
      Debug.Log("Collision Enter");
      sound1.Play();
      GetComponent(ScoreScript).score += kunai;
      Destroy(this.gameObject);
 
  }
avatar image Jan182 Positive7 · Sep 11, 2015 at 12:07 PM 0
Share

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;
        
   }
avatar image Positive7 Jan182 · Sep 11, 2015 at 12:32 PM 0
Share

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); 
Show more comments

3 Replies

· Add your reply
  • Sort: 
avatar image
0

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Jan182 · Sep 11, 2015 at 12:24 PM 0
Share

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

avatar image
0

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>();
 }

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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;

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

33 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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

Trouble understanding why GameObject.Find works but declaring/initializing a GameObject and using GetComponent does not 1 Answer

Random audio clips from array on prefab crash 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges