Instantiating object sometimes instantiates with disabled script and collider
Hi I noticed that some of my objects have both disabled scripts and disabled colliders upon instantiation. The only way I can solve this is by simply checking if they are disabled right after instantiating the object and then enabling them. No where in my program am I disabling things. The problem is at about line 100. EDIT: I did some testing and it appears this happens when the object that is instantiating gets destroyed right as it is instantiating the new object. I think it is because they all have timers that activate their code only every few frames to improve performance.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class plant1 : MonoBehaviour
{
public GameObject plant1Prefab;
public GameObject deadPlant1Prefab;
[System.NonSerialized]
public Color color;
float colorMutationAmount = .05f;
float lifeSpan = 3600; // one hour in seconds
[System.NonSerialized]
public float fruitingAge = 250;
[System.NonSerialized]
public float growRate = 1;
[System.NonSerialized]
public float age = 0;
int reproduceDelay = 50;
float reproduceDelayCounter = 0;
RaycastHit2D hit;
Vector3 dir;
public LayerMask sightCollidersLayerMask;
float xDir = 1f;
float yDir = 1f;
bool b = true;
[System.NonSerialized]
public float size = 1;
float seedSpreadDistance = .5f;
[System.NonSerialized]
public bool growing = true;
[System.NonSerialized]
public bool reproducing = true;
float timeMul;
float edgeLimits = 100;
int frameSkipCounter = 0;
int frameSkip = 30;
void Start()
{
frameSkipCounter = Random.Range(0, frameSkip);
timeMul = GameObject.Find("Manager").GetComponent<PopulationManager>().timeMul;
color = GetComponent<SpriteRenderer>().color;
mutate(1, 1);
if (Mathf.Abs(transform.position.y) > edgeLimits || Mathf.Abs(transform.position.x) > edgeLimits)
{
Destroy(gameObject);
}
}
void FixedUpdate()
{
frameSkipCounter++;
if (frameSkipCounter >= frameSkip)
{
lookAroundRayCast();
age += growRate * timeMul * Time.deltaTime * frameSkip;
reproduceDelayCounter += timeMul * Time.deltaTime * frameSkip;
frameSkipCounter = 0;
if (growing)
{
if (age >= fruitingAge)
{
growing = false;
}
else
{
float s = (size * (age / fruitingAge)) + 0.1f;
transform.localScale = new Vector3(s, s, 1);
}
}
if (!growing && reproduceDelayCounter >= reproduceDelay)
{
reproduceDelayCounter = 0;
if (reproducing)
{
Vector3 spawnDir = new Vector3(Random.Range(-1f, 1f), Random.Range(-1f, 1f));
spawnDir.Normalize();
hit = Physics2D.Raycast(transform.position + spawnDir * .12f, spawnDir, .5f, ~sightCollidersLayerMask);
if (!hit)
{
GameObject seedling = Instantiate(plant1Prefab, transform.position + spawnDir * .5f, Quaternion.identity);
seedling.transform.localScale = new Vector3(.1f, .1f, 1);
seedling.GetComponent<SpriteRenderer>().color = color;
seedling.GetComponent<plant1>().size = size;
seedling.name = name;
if (seedling.GetComponent<plant1>().enabled == false)
{
Debug.Log("MADE CHILD WITH DISABLED SCRIPT!!!!!!!!"); // WHY!?!?!?!?
seedling.GetComponent<plant1>().enabled = true;
seedling.GetComponent<Collider2D>().enabled = true;
seedling.GetComponent<SpriteRenderer>().color = Color.black;
}
}
}
}
if (age > lifeSpan)
{
GameObject deadArea = Instantiate(deadPlant1Prefab, transform.position, Quaternion.identity);
deadArea.GetComponent<deadPlant1>().color = color;
Destroy(gameObject);
}
}
}
void mutate(float curve, float rate)
{
color.r += Random.Range(-colorMutationAmount, colorMutationAmount);
color.g += Random.Range(-colorMutationAmount, colorMutationAmount);
color.b += Random.Range(-colorMutationAmount, colorMutationAmount);
//size += Random.Range(-.01f, .01f);
growRate += Random.Range(-.1f, .1f);
}
void lookAroundRayCast()
{
float resolution = .25f;
for (int i = 0; i < 4/resolution; i++)
{
if (b && yDir > -1)
{
yDir -= resolution;
if (yDir <= -1)
{
yDir = resolution;
}
}
else if (b && yDir <= -1)
{
xDir -= resolution;
if (xDir <= -1)
{
xDir = resolution;
b = false;
}
}
else if (!b && yDir < 1)
{
yDir += resolution;
if (yDir >= 1)
{
yDir = 1f;
}
}
else if (!b && yDir >= 1)
{
xDir += resolution;
if (xDir >= 1)
{
xDir = 1f;
b = true;
}
}
dir = new Vector3(xDir, yDir);
hit = Physics2D.Raycast(transform.position + dir * .12f, dir, .14f, ~sightCollidersLayerMask);
if (hit)
{
if (hit.transform.tag == "plant1")
{
// two plants colliding
float colorDifference = 0;
Color otherPlantColor = hit.transform.GetComponent<plant1>().color;
colorDifference += Mathf.Abs(color.r - otherPlantColor.r) + Mathf.Abs(color.g - otherPlantColor.g) + Mathf.Abs(color.b - otherPlantColor.b);
if (colorDifference > .25f) // competing subspecies
{
if (hit.transform.GetComponent<plant1>().growRate < growRate)
{
Destroy(hit.transform.gameObject);
}
if (hit.transform.GetComponent<plant1>().growRate > growRate)
{
Destroy(gameObject);
}
}
else
{
if (age < hit.transform.gameObject.GetComponent<plant1>().age)
{
//GameObject deadArea = Instantiate(deadPlant1Prefab, transform.position, Quaternion.identity);
//deadArea.GetComponent<SpriteRenderer>().color = plantColor;
Destroy(gameObject);
}
}
}
if (hit.transform.tag == "deadPlant1")
{
// destroy if color difference is less than .1
float colorDifference = 0;
Color deadPlantColor = hit.transform.GetComponent<deadPlant1>().color;
colorDifference += Mathf.Abs(color.r - deadPlantColor.r) + Mathf.Abs(color.g - deadPlantColor.g) + Mathf.Abs(color.b - deadPlantColor.b);
if (colorDifference < 2f)
{
Destroy(gameObject);
}
else
{
Destroy(hit.transform.gameObject);
}
}
}
}
/*
dir = new Vector3(0, 1);
Vector3 pointAtEndOfDir = transform.position + dir * .15f;
LineRenderer l = gameObject.GetComponent<LineRenderer>();
List<Vector3> pos = new List<Vector3>();
pos.Add(transform.position + dir * .12f);
pos.Add(pointAtEndOfDir);
l.startWidth = .05f;
l.endWidth = .05f;
l.SetPositions(pos.ToArray());
l.useWorldSpace = true;
*/
}
}
Your answer
Follow this Question
Related Questions
Instantiate() Argument error 1 Answer
Trying to replace 2 objects. Destroying assets is not permitted to avoid data loss. 0 Answers
*Solved* Loop instantiating objects causes my Unity Editor to crash 1 Answer
Instantiated button prefab causes Delegation error on click and does not add OnClick Listener 0 Answers
SpaceShooter Tutorial instanced asteroid not taking GameController game object in unity5 0 Answers