Script component seems to be missing when spawning prefab?
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<SpawnDataScript> ();
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<MovementScript>().special);
Debug.Log (GameObject.FindGameObjectWithTag ("Player").name);
Debug.Log (GameObject.FindGameObjectWithTag ("Player").GetComponent<MovementScript>().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?
It seems that prefab is not updated after changing object. Select the instance of prefab in inspector and click the 'Save' button above Transform component.
There is no save button in the prefab- probably because I never changed the object.
Are there any other issues which could cause this problem? I can post a download link to the project if it will help.
Here's the project I'm working on: http://www.mediafire.com/download/2k1htu34zmq6125/Dragon_Ball_R.zip Let me know if there's anything you need explained.
Answer by Maruder · Sep 22, 2015 at 11:12 PM
You are getting that error probably because you destroy the player gameobject that holds the MovementScript.
Destroy (GameObject.FindGameObjectWithTag ("Player"));
Probably remove that line of code above and your problem may be solved. Good luck.
That code is run before the player GameObject is being created. I have a dummy GameObject with the player tag in the game, so that other objects that may run their start functions first don't crash the game.
And, like I said in the original question, the player isn't missing. I can see it on screen.
Regardless, I tried commenting out that line, but nothing was fixed.
Answer by snuffysam · Sep 25, 2015 at 05:45 PM
I think I'll be creating a new question soon, since clearly the bigger problem (and likely the cause of this one) is that prefabs won't save.
Your answer
Follow this Question
Related Questions
Why do I keep getting 'The object you want to instantiate is null' warning? 1 Answer
Trying to spawn enemies on only one path 1 Answer
Networking - How do i spawn an object with client authority? 1 Answer
Spawning limited GameObjects at a specific position not working 1 Answer
spawn object every 10 score points 1 Answer