Quantcast
Channel: Questions in topic: "error"
Viewing all articles
Browse latest Browse all 7934

Script component seems to be missing when spawning prefab?

$
0
0
I'm making a racing game, and trying to spawn the characters into the stage as the match starts. The spawning object has an array named racers, which contains all the prefabs for the characters, and an array named spawners, which has all the locations where racers can spawn. There is also a game object called "GameController" which has all the data for what players should spawn. public class SpawnRacers : MonoBehaviour { public GameObject[] racers; public GameObject[] spawners; // Use this for initialization void Start () { Destroy (GameObject.FindGameObjectWithTag ("Player")); var r = GameObject.FindGameObjectWithTag ("GameController").GetComponent (); int start = (spawners.Length - r.GetPlayers ().Length) / 2; for (int i = 0; i < r.GetPlayers ().Length; i++) { GameObject g = (GameObject)Instantiate (racers [r.GetPlayers () [i] - 1], spawners [start + i].transform.position, Quaternion.identity); g.transform.Rotate(new Vector3(0,-100,0)); if (i < r.GetHuman ()) { g.tag = "Player"; GameObject.FindGameObjectWithTag ("MainCamera").transform.SetParent (g.transform); } else { g.tag = "Racer"; } Debug.Log (g.tag); Debug.Log (g.name); Debug.Log (g.GetComponent().special); Debug.Log (GameObject.FindGameObjectWithTag ("Player").name); Debug.Log (GameObject.FindGameObjectWithTag ("Player").GetComponent().special); } } The player is spawning in the right place, and most of the Debug.Log's at the bottom print the correct result, but the player won't move, and the error "Object reference not set to an instance of an object" shows up when running lines 27 and 29 (the ones that try to access MovementScript). From this it seems like MovementScript is missing on my player's prefab, but it's definitely there in the inspector... what's going on?

Viewing all articles
Browse latest Browse all 7934

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>