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 revelentguy05 · May 19, 2021 at 12:40 PM · gameobjectunity 2d

GameObject with a tag destroy themselves after exactly 1 minute 1 second for no reason

In my game I have 3 objects with the tag "Body" after running this script (see below) everything works perfectly as intended. Unfortunately, after using debug.log i can tell you that after 1.01 minutes the gameobjects with the tag "Body" are deleted. I have no idea if this is a glitch or bad code:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class OrbitPathFinder : MonoBehaviour
 {
     public int numSteps = 1000;
     public float timeStep = 0.1f;
     public float gravitationalConstant;
     public bool relativeToBody;
     public GameObject centralBody;
     private GameObject[] bodies;
     private bool gameOn = false;
     void Start()
     {
         bodies = GameObject.FindGameObjectsWithTag("Body");
         
     }
     void Update()
     {
         if(Input.GetKeyDown("space"))
         {
             gameOn = true;
         }
         if(gameOn)
         {
             HideOrbits();
         }
         else
         {
             DrawOrbits();
         }
     }
 
     void DrawOrbits()
     {
         var virtualBodies = new VirtualBody[bodies.Length];
         var drawPoints = new Vector3[bodies.Length][];
         int referenceFrameIndex = 0;
         Vector3 referenceBodyInitialPosition = Vector3.zero;
 
         for (int i = 0;i < virtualBodies.Length;i++)
         {
             virtualBodies[i] = new VirtualBody (bodies[i]);
             drawPoints[i] = new Vector3[numSteps];
 
             if (bodies[i] == centralBody && relativeToBody) { 
                 referenceFrameIndex = i;                       
                 referenceBodyInitialPosition = virtualBodies[i].position; 
             }
         }
 
         for (int step = 0; step < numSteps; step++) {  //for each step simulate the virtual bodies
             Vector3 referenceBodyPosition = (relativeToBody) ? virtualBodies[referenceFrameIndex].position : Vector3.zero;//the position of the reference body in every step
             // Update velocities
             for (int i = 0; i < virtualBodies.Length; i++) {  //for each virtual body
                 virtualBodies[i].velocity += CalculateAcceleration (i, virtualBodies) * timeStep; //increases virtual objects velocity by the acceleration * speed of calculation
             }
             // Update positions
             for (int i = 0; i < virtualBodies.Length; i++) { //for each virtual object 
                 Vector3 newPos = virtualBodies[i].position + virtualBodies[i].velocity * timeStep; //new position is equal to the position and velocity times the time step
                 virtualBodies[i].position = newPos; //the virtual bodies position is then updated to the new position
                 if (relativeToBody) { //if there is a central body
                     var referenceFrameOffset = referenceBodyPosition - referenceBodyInitialPosition;// the offset of the reference frame is the new position - initialposition
                     newPos -= referenceFrameOffset; //the offset is then subtracted from the new position
                 }
                 if (relativeToBody && i == referenceFrameIndex) { //if relative to body and the reference frame is the current index
                     newPos = referenceBodyInitialPosition; //then the position will be the initalposition
                 }
 
                 drawPoints[i][step] = newPos; //The point where the line will be drawn then has another value added
             }
         }
 
         for (int bodyIndex = 0; bodyIndex < virtualBodies.Length; bodyIndex++) 
         {
                 var pathColour = bodies[bodyIndex].gameObject.GetComponent<TrailRenderer> ().sharedMaterial.color; //sets the path to the colour of the object
 
                 for (int i = 0; i < drawPoints[bodyIndex].Length - 1; i++) { //for each bodies array of draw points length - 1
                     Debug.DrawLine (drawPoints[bodyIndex][i], drawPoints[bodyIndex][i + 1], pathColour);//draws a line on each draw point
                     Debug.Log("yes");
                 var lineRenderer = bodies[bodyIndex].gameObject.GetComponentInChildren<LineRenderer> ();
                 if (lineRenderer) {
                     lineRenderer.enabled = false;  //disables the linerenderer in the child object of the planet
                 }
             }
 
         }
 
     }
 
     void HideOrbits()
     {
         for (int bodyIndex = 0; bodyIndex < bodies.Length; bodyIndex++) { //for each body
             var lineRenderer = bodies[bodyIndex].gameObject.GetComponentInChildren<LineRenderer> (); 
             lineRenderer.positionCount = 0; //sets the amount of positions to 0
         }
     }
 
     Vector3 CalculateAcceleration (int i, VirtualBody[] virtualBodies) {
         Vector3 acceleration = Vector3.zero;//acceleration = 0
         for (int j = 0; j < virtualBodies.Length; j++) {//foreach virtual body
             if (i == j) { //if the index of the virtual body is the same as the real objects then continue
                 continue;//    /\
             }       //         |
             Vector3 forceDir = (virtualBodies[j].position - virtualBodies[i].position).normalized;
             float sqrDst = (virtualBodies[j].position - virtualBodies[i].position).sqrMagnitude;
             acceleration += forceDir * gravitationalConstant * virtualBodies[j].mass / sqrDst;
         }   //find the acceleration of the vitual object at that point using gravity formula
         return acceleration;//return the value of the acceleration
     }
 
     class VirtualBody
     {
         public Vector3 position;
         public Vector3 velocity;
         public float mass;
 
         public VirtualBody(GameObject g)
         {
             position = g.transform.position;
             velocity = g.GetComponent<gravity>().startVelocity;
             mass = g.GetComponent<gravity>().mass;
         }
     }
 }

Comment
Add comment
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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by revelentguy05 · May 19, 2021 at 12:46 PM

Ok ignore that part somehow the time on the trail renderer is destroying the object because it is not moving

Comment
Add comment · Show 1 · 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 revelentguy05 · May 19, 2021 at 09:27 PM 0
Share

I had autodestruct turn on the line renderer :( don't worry

avatar image
0

Answer by Adil_Alhilali · May 19, 2021 at 12:49 PM

At first glance nothing in the code should delete any object, are you sure there is no other script is active in 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

182 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

Related Questions

why gameobject / raycast when dragged is not accurately following the touch position in unity 0 Answers

Game Object Not Working Properly 0 Answers

is it Possible to use navigation on non UI elements? 1 Answer

Don't Destroy on Load not working for me 1 Answer

Prefab Objects do not Render in Game mode but do In Scene Mode,Flappy Bird tutorial - columns in prefab are not showing in game view 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