Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by Marshal_Alessi · Nov 18, 2016 at 10:11 AM · c#transformposition

Object Reference not Set

I'm having an issue with my script not finding a gameobjects position, and I believe it's how I'm setting it but I'm not quite sure how to do it properly. First, here are my two declarations. Please note that the players variable is set using GetAllGameObjectsWithTag("Players")

     GameObject[] players;
     public Vector3[] _pos;

...

         for (int x = 0; x < players.Length; x++)
         {
             players[x].GetComponent<Animator>().SetBool("MovingLeft", false);
             players[x].GetComponent<Animator>().SetBool("MovingRight", false);
             _pos[x] = players[x].gameObject.transform.position;
         }

This first two lines that set the bool work just fine, so I've got the right game objects, but no matter how I set the _pos[x], it always errors saying it can't find it. How do I set the position to the corresponding game object?(Also, I really only need the 'x' value of position though either way is fine)

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 Marceta · Nov 18, 2016 at 10:30 AM 1
Share

By looking at your code right now players and _pos arrays must have same number of indexes so this code can work. For example:

If there are 5 players and there are 3 positions you will get error because _pos[4] is null. You have only 3 positions.

 _pos[0] = players[0]..gameObject.transform.position; O$$anonymous$$
 _pos[1] = players[1]..gameObject.transform.position; O$$anonymous$$
 _pos[2] = players[2]..gameObject.transform.position; O$$anonymous$$
 _pos[3] = players[3]..gameObject.transform.position; ERROR
 _pos[4] = players[4]..gameObject.transform.position; ERROR

avatar image roman_sedition · Nov 18, 2016 at 11:21 AM 0
Share

@$$anonymous$$arshal_Alessi

 _pos[x] = players[x].gameObject.transform.position;

but players is already an array for GameObjects.

so this should do ins$$anonymous$$d

 _pos[x] = players[x].transform.position;

I think the problem is that you haven't defined how many elements can go into your array. It is usually best to use Lists if you are working with an undefined capacity for a collection as Lists have dynamic capacity. Here is a workaround though, but you still need

 using System.Collections.Generic;

 List<Vector3> positions = new List<Vector3>();
         foreach(GameObject player in players) 
         {
             player.GetComponent<Animator>().SetBool("$$anonymous$$ovingLeft", false);
             player.GetComponent<Animator>().SetBool("$$anonymous$$ovingRight", false);
             positions.Add (player.transform.position);
         }
         _pos = positions.ToArray ();

This works because we are assigning _pos with a finite amount of Vector3s, it uses a List.ToArray() method which returns an array for you which can be assigned to another array. $$anonymous$$uch like how you can assign

 players = GameObject.FindGameObjectsWithTag("Player");




1 Reply

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

Answer by Marceta · Nov 18, 2016 at 10:39 AM

If I understood you correctly you want to collect positions of all players. You could do it like this:

 GameObject[] players;
      public List<Vector3> _pos = new List<Vector3>();
     for (int x = 0; x < players.Length; x++)
              {
                  players[x].GetComponent<Animator>().SetBool("MovingLeft", false);
                  players[x].GetComponent<Animator>().SetBool("MovingRight", false);
                  _pos.Add(players[x].gameObject.transform.position);
              }

Comment
Add comment · Show 2 · 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
avatar image Marshal_Alessi · Nov 18, 2016 at 10:51 AM 0
Share

This worked, but I'm still not sure why $$anonymous$$e did not. You are correct that they are corresponding arrays, yet $$anonymous$$e failed to find the player with the position. Do you have any idea as to why that might have happened?

avatar image Marceta Marshal_Alessi · Nov 18, 2016 at 10:56 AM 0
Share

$$anonymous$$ake those two variables public (this will show them on inspector) and check in play mode are they same length (same number of items). They are probably not same length that's why you are getting error. Because if you are trying to access _pos[5] and _pos have only 3 items you will get error because _pos[5] doesn't exist and it's null. So you are trying to set value to something that doesn't exist.

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

244 People are following this question.

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

Related Questions

Vector3.Lerp not working properly, making the player bounce around 2 Answers

Make object move back and forth 2 Answers

Need help with this code? thanks. 2 Answers

Rotation set to face direction of movement but doesn't always work 1 Answer

i need to change box position in y axis if i hit from down (c# script) 2d 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