Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by vipvex · May 13, 2012 at 11:03 PM · error

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
             

             }
         
         
         }

         
     }
     

NullReferenceException: Object reference not set to an instance of an object

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)
Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image syclamoth · May 13, 2012 at 11:06 PM 1
Share

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.

avatar image Drakestar · May 14, 2012 at 03:38 AM 0
Share

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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.");
     }
 }
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

7 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges