- Home /
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
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
I had autodestruct turn on the line renderer :( don't worry
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?