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 /
avatar image
0
Question by Shujahat-Murtaza · Nov 16, 2016 at 07:27 AM · racedie

Position Calculation after player dies

I got Stuck Help me . In game player Car has weapons . When it destroys opponent vehicle is no more position calculation using WayPionts occur . I get error of reference execption .

( MissingReferenceException: The object of type 'GameObject' has been destroyed but you are still trying to access it.Your script should either check if it is null or you should not destroy the object.)

I want that when payer is destroyed from scene it also removed from racerinfo[i] array . If anyone know how to do that plz tell me .

I am using that function for position Calculation.

void CalculateRacerPositions() { canCalculatePosition = false;

     if (Application.isPlaying && gameStarted) {
         float distance;
         for (int i = 0; i < racerInfo.Length; i ++) {
             
                 racerInfo [i].checkpointDistance = Vector3.Distance (racerInfo [i].racer.transform.position, racerInfo [i].currentWaypoint.position);
             distance = (racerInfo [i].checkpointDistance / 500);
             racerInfo [i].distanceScore = - (distance * distance);
             racerInfo [i].totalScore = racerInfo [i].positionScore + racerInfo [i].distanceScore;
             for(int i2 = 0; i2 < raceData.numberOfRacers[raceData.raceNumber]; i2++){
                 toSort[i2] = racerInfo [i2].position;
             }
             foreach (int sort in toSort.OrderBy(sorted=>sorted)) {
                 if (racerInfo [i].position == sort) {
                     managerReference.positionText [sort - 1].text = sort + "   " + racerInfo [i].racerName;
                 }
             }
         }
         for(int i2 = 0; i2 < raceData.numberOfRacers[raceData.raceNumber]; i2++){
             toSort2[i2] = racerInfo [i2].totalScore;
         }
         for (int i = 0; i < racerInfo.Length; i ++) {
             var sort2 = toSort2.OrderByDescending(sorted=>sorted).ToArray();
             float scoreCheck = racerInfo[i].totalScore;
             for (int i2 = 0; i2 < toSort2.Length; i2 ++) {
                 if (scoreCheck == sort2 [i2]) {
                     racerInfo [i].position = i2 + 1;
                 }
             }
         }
     }
     //asdad
     if(raceData.numberOfRacers[raceData.raceNumber] > 8){
         if (racerInfo [0].position > 8) {
             for (int i = 6; i < raceData.numberOfRacers[raceData.raceNumber] + 1; i++) {
                 if (racerInfo [0].position != i) {
                     managerReference.positionObject [i - 1].gameObject.SetActive (false);

                 } else {
                     Vector3 tPos;
                     tPos = managerReference.positionObject [i - 1].localPosition;
                     tPos.y = -230;
                     managerReference.positionObject [i - 1].localPosition = tPos;
                     managerReference.positionObject [i - 1].gameObject.SetActive (true);

                     tPos = managerReference.positionObject [i - 2].localPosition;
                     tPos.y = -160;
                     managerReference.positionObject [i - 2].localPosition = tPos;
                     //managerReference.positionObject [i - 2].transform.localPosition.y = -160;
                     managerReference.positionObject [i - 2].gameObject.SetActive (true);

                     tPos = managerReference.positionObject [i - 3].localPosition;
                     tPos.y = -90;
                     managerReference.positionObject [i - 3].localPosition = tPos;
                     //managerReference.positionObject [i - 2].transform.localPosition.y = -90;
                     managerReference.positionObject [i - 3].gameObject.SetActive (true);
                 }
             }
         } else {
             for(int i = 0; i < raceData.numberOfRacers[raceData.raceNumber]; i++){
                 if (i <= 7) {
                     Vector3 tPos;
                     if (i == 6) {
                         tPos = managerReference.positionObject [i].localPosition;
                         tPos.y = -160;
                         managerReference.positionObject [i].localPosition = tPos;
                     }
                     if (i == 7) {                            
                         tPos = managerReference.positionObject [i].localPosition;
                         tPos.y = -230;
                         managerReference.positionObject [i].localPosition = tPos;
                     }
                     managerReference.positionObject [i].gameObject.SetActive (true);
                 } else {
                     managerReference.positionObject [i].gameObject.SetActive (false);
                 }
             }
         }
     }
     canCalculatePosition = true;
 }

Destruction done in Health script on Opponents vehicle by calling reduceHealth(Damage)

If anyone know solution to this problem plz help .

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 Dibbie · Nov 16, 2016 at 09:24 AM 0
Share

So, in your "reduceHealth", if the player's health is zero, you Destry(racerInfo[positionOfYourPlayerInThisArray].gameObject); ins$$anonymous$$d.

Another alternative, and what you should probably do regardless, is in your script here, do if-checks before anything -- if(racerInfo[i] != null){whatever code should happen} that way, if it does happen to come across something that was JUST destroyed in its loop, or the player who was destroyed at an unknown index, it will just skip over it and you wont get that error anymore.

avatar image Shujahat-Murtaza Dibbie · Jan 18, 2017 at 07:49 AM 0
Share

Thanks . For your replay i will implement it . :)

0 Replies

· Add your reply
  • Sort: 

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

How Do I Set The Number Of Laps? 1 Answer

How do I download the new unity 5 car tutor? 1 Answer

Mario first person game, help? 1 Answer

Using iTween for a race ? 2 Answers

Logic on calculating positions in a race. 5 Answers


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