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

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:

#pragma strict

var destello : Light = this.gameObject.GetComponent(Light);
var proyectil : Rigidbody;
var velocidad : float = 20f;
var duracion : float = 0.5f;

function Start () {
    destello.enabled = true;
    var inGameProyectil : Rigidbody = Instantiate( proyectil, transform.position, transform.rotation );
    inGameProyectil.velocity = transform.TransformDirection( Vector3( 0, 0, velocidad ) );
    Physics.IgnoreCollision( inGameProyectil.GetComponent.<Collider>(), transform.root.GetComponent.<Collider>() );
    yield WaitForSeconds(duracion);
    destello.enabled = false;
}

The error which are showed by Unity is the next:
get_gameObject 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.

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:

using UnityEngine;
using System.Collections;
using UnityStandardAssets.CrossPlatformInput;

public class ShootM9 : MonoBehaviour {

    private Animator animacionDisparo;
    private bool isShooting;
    public GameObject shooter;

    void Start () {
        animacionDisparo = this.gameObject.GetComponent<Animator>();
        isShooting = false;
        shooter.SetActive(false);
    }
  
    void Update () {
        if (CrossPlatformInputManager.GetButtonDown("Shoot") && isShooting==false) {
            isShooting = true;
            shooter.SetActive(true);
            StartCoroutine("disparos");
        }
    }
    IEnumerator disparos() {
        animacionDisparo.enabled = true;
        yield return new WaitForSeconds(0.1666666f);
        animacionDisparo.enabled = false;
        shooter.SetActive(false);
        isShooting = false;
    }
}

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 :slight_smile:

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:

IEnumerator WaitSeconds(int seconds) {
    yield return new WaitForSeconds(seconds);
}

And then just start the coroutine in the Start(); method.

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

Thank for all, but nothing works, I think that I’m going to create a new diferent code.