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 ![]()