Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

[Error] get_gameObject can only be called from the main thread

Discussion in 'Scripting' started by Miguecraft, Oct 12, 2015.

  1. Miguecraft

    Miguecraft

    Joined:
    Oct 12, 2015
    Posts:
    5
    Sorry about this, I don't know to program (and I'm not very well writing in English, so I'm gonna try to explain it).

    I was making a "Shoot System" for a game, and when I try to run this script, Unity's Console show an error:

    Code (JavaScript):
    1. #pragma strict
    2.  
    3. var destello : Light = this.gameObject.GetComponent(Light);
    4. var proyectil : Rigidbody;
    5. var velocidad : float = 20f;
    6. var duracion : float = 0.5f;
    7.  
    8. function Start () {
    9.     destello.enabled = true;
    10.     var inGameProyectil : Rigidbody = Instantiate( proyectil, transform.position, transform.rotation );
    11.     inGameProyectil.velocity = transform.TransformDirection( Vector3( 0, 0, velocidad ) );
    12.     Physics.IgnoreCollision( inGameProyectil.GetComponent.<Collider>(), transform.root.GetComponent.<Collider>() );
    13.     yield WaitForSeconds(duracion);
    14.     destello.enabled = false;
    15. }
    The error which are showed by Unity is the next:

    I know that it's in the Start function, but it's because the GameObject whose have this script is activated for another script, this one:

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using UnityStandardAssets.CrossPlatformInput;
    4.  
    5. public class ShootM9 : MonoBehaviour {
    6.  
    7.     private Animator animacionDisparo;
    8.     private bool isShooting;
    9.     public GameObject shooter;
    10.  
    11.     void Start () {
    12.         animacionDisparo = this.gameObject.GetComponent<Animator>();
    13.         isShooting = false;
    14.         shooter.SetActive(false);
    15.     }
    16.  
    17.     void Update () {
    18.         if (CrossPlatformInputManager.GetButtonDown("Shoot") && isShooting==false) {
    19.             isShooting = true;
    20.             shooter.SetActive(true);
    21.             StartCoroutine("disparos");
    22.         }
    23.     }
    24.     IEnumerator disparos() {
    25.         animacionDisparo.enabled = true;
    26.         yield return new WaitForSeconds(0.1666666f);
    27.         animacionDisparo.enabled = false;
    28.         shooter.SetActive(false);
    29.         isShooting = false;
    30.     }
    31. }
    32.  
    I read other post of people who has the same problem that I, and the people said "There isn't actually any master script or anything; the error is referring to that particular script.".

    I want to know if somebody can solve it and how.

    Thank you very much :)
     
  2. Carvuh

    Carvuh

    Joined:
    Mar 25, 2013
    Posts:
    25
    Code (csharp):
    1. yield return new WaitForSeconds();
    Cannot be run in an method by just calling it. You must place it in a coroutine and start it in the method itself I. But be warned, if you start a coroutine inside an update method, it's just going to run the method 60+ times a second.

    If you NEED to wait seconds you can create something like:

    Code (csharp):
    1. IEnumerator WaitSeconds(int seconds) {
    2.     yield return new WaitForSeconds(seconds);
    3. }
    4.  
    And then just start the coroutine in the Start(); method.
     
  3. BenZed

    BenZed

    Joined:
    May 29, 2014
    Posts:
    524
    You are assigning a value to then Light variable 'destello' by accessing the gameObject.

    You cant do this in the class declaration, you have to this at runtime.

    Place 'gameObject.GetComponent(Light)' in Awake() or Start()
     
  4. Miguecraft

    Miguecraft

    Joined:
    Oct 12, 2015
    Posts:
    5
    Thank for all, but nothing works, I think that I'm going to create a new diferent code.