- Home /
Script error help ?
My script worked fine for like 10 minutes and then stopped working. I didn't change it at all and i get the same error every time. Could some one please explain why my script doesn't work?
function GetActions(){
var players = GameObject.FindGameObjectsWithTag ("Player"); // Fineds all the players in the scene
for (var p : GameObject in players){ // sets p as a game object for every player in the scene
var PlayerComponent : Component = p.GetComponent("Player"); // Gets the component of every player in the scene
var PlayerTransform : Transform = p.GetComponent(Transform);
Debug.Log("I found " + players.Length + " players");
if (PlayerComponent.turnEnded){ // If the current player made a move
if (PlayerComponent.NextTurnMove){ // if the player moved for his turn
PlayerTransform.position = PlayerComponent.nextMovingPosition; // get the position he wants to move to
PlayerComponent.turnEnded = false; // allows the player to move for the next turn
}
}
}
Boo.Lang.Runtime.RuntimeServices.GetDispatcher (System.Object target, System.String cacheKeyName, System.Type[] cacheKeyTypes, Boo.Lang.Runtime.DynamicDispatching.DispatcherFactory factory) Boo.Lang.Runtime.RuntimeServices.GetDispatcher (System.Object target, System.Object[] args, System.String cacheKeyName, Boo.Lang.Runtime.DynamicDispatching.DispatcherFactory factory) Boo.Lang.Runtime.RuntimeServices.GetProperty (System.Object target, System.String name) UnityScript.Lang.UnityRuntimeServices.GetProperty (System.Object target, System.String name) Turn.GetActions () (at Assets/Scripts/Turn.js:69) Turn.Update () (at Assets/Scripts/Turn.js:29)NullReferenceException: Object reference not set to an instance of an object
You need to tell us what line the error is occuring on. I suspect it is happening because of GetComponent failing at runtime- one of the objects in the array returned my FindWithTag doesn't have the right things attached to it.
What syclamoth said. And you should always check whether the result is null (or empty, in case of an array/list) when doing dynamic object searches like GameObject.Find. That way you don't run into null references later. In this case, add if (players.Count != 0) { for (var p : GameObject in players)...
Answer by bompi88 · May 14, 2012 at 08:29 AM
Try this. Put your FindGameObjectsWithTag in the start function, so it has some time to initialize. And check if it found some players, if not: print out an error "Could not find any players...". This should work if you have a script "Player" on your players.
var players : GameObject[];
function Start() {
players = GameObject.FindGameObjectsWithTag ("Player"); // Finds all the players in the scene
}
function GetActions() {
Debug.Log("I found " + players.Length + " players");
if (players.Length != 0) {
for (var p : GameObject in players) { // sets p as a game object for every player in the scene
// Player is a script (class) called Player?
// You don't need the " in getcomponent if your Player script is in the same language
// And it returns a Player-object
var PlayerComponent : Player = p.GetComponent(Player); // Gets the component of every player in the scene
var PlayerTransform : Transform = p.GetComponent(Transform);
if (PlayerComponent.turnEnded) { // If the current player made a move
if (PlayerComponent.NextTurnMove) { // if the player moved for his turn
PlayerTransform.position = PlayerComponent.nextMovingPosition; // get the position he wants to move to
PlayerComponent.turnEnded = false; // allows the player to move for the next turn
}
}
}
} else {
Debug.LogError("Could not find any players. Try to add a player to the scene.");
}
}
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Errors with script using Javascript. 2 Answers
please help NullReferenceException object 1 Answer
ERROR unity expected. insert a semicolon at the end 2 Answers
BCE0044: expecting EOF, found '}'. 1 Answer