• 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 captainspaceman · Jan 08 at 10:15 PM · errorinstantiatedisabled

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;
         */
     }
 }
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

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

211 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

Related Questions

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

I need help with instantiate - Overloads 3 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges