Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

(Possible unity bug) Why is my object spawning disabled?

Discussion in 'Scripting' started by Jorjor210, Jun 10, 2015.

  1. Jorjor210

    Jorjor210

    Joined:
    May 3, 2015
    Posts:
    91
    Im currently making a script that makes a object spawn when the player collides with a object. But when the object spawns it spawns with it's box collider and scripts disabled? any ideas why? (the prefab has everything enabled. It's just when it spawns it all becomes disabled).
     

    Attached Files:

  2. GroZZleR

    GroZZleR

    Joined:
    Feb 1, 2015
    Posts:
    3,201
    Can you post the code where you spawn the prefab?
     
  3. Jorjor210

    Jorjor210

    Joined:
    May 3, 2015
    Posts:
    91
    I know I probably should use Vector 2, but the current game I'm making is a improved version of my first attempt, and my other attempt dealt with 3D objects. I will make little fixes like that when i get it working. But I don't think that contributes to the problem at hand.
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class random : MonoBehaviour
    5. {
    6.     public GameObject prefab;
    7.  
    8.     void OnTriggerEnter2D (Collider2D info)
    9.     {
    10.         if (info.name == "Player")
    11.         {
    12.             SpawnCoin();
    13.         }
    14.     }
    15.  
    16.     void SpawnCoin()
    17.     {
    18.         Vector3 position = new Vector3(Random.Range(-9f, 9F), Random.Range(-5.56F, 5.27F), 0);
    19.         Instantiate (prefab, position, Quaternion.identity);
    20.     }
    21. }
     
  4. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    sidenote: having a class called "random" when there is already a class called "Random" within the engine is just crying out for misnaming problems and confusion later... try to be more descriptive with your names, "RandomCoinSpawner" or something would make things a lot clearer. Also, generally classes have capital first letters and variables are lowercase first letters, so another layer of potential confusion :confused::confused:

    http://docs.unity3d.com/ScriptReference/Random.html

    you could even go with making all the hardcoded values public variables and rename the function to just "Spawn" and then you just have a "RandomSpawner" which can be used for anything... :D



    does the coin have a script running on it? there isn't one in the images above, what is that doing in start/awake?
     
  5. Jorjor210

    Jorjor210

    Joined:
    May 3, 2015
    Posts:
    91
    The object has 3 scripts on it, the image above was supposed to show the collider being disabled on spawn not the scripts (although the scripts also get disabled). the 3 scripts do: the first script attached is another OnTriggerEnter2D script which destroys the current coin if it collides with player (but since the collider for some reason is disabled once it spawns it doesn't work). the second script attached is the only script with a Start function attached to it. this script is basically the same as the other spawner script, but this script just spawns the coin at a random place on the map right in the beginning. (this script isnt attached directly to the object, it's attached to the gameManager, but this script actually works properly, it seems anything that spawns right in the start works, but after that it doesn't.) and then the third script is the one mentioned above.
     
  6. Jorjor210

    Jorjor210

    Joined:
    May 3, 2015
    Posts:
    91
    Please also keep in mind This script works in the same exact game I've made previously. This game i'm making now is just a improved version. the only real difference is now I'm using 2D object rather than 3D objects.
     
  7. Jorjor210

    Jorjor210

    Joined:
    May 3, 2015
    Posts:
    91
    I'm beginning to wonder if this is a unity problem.
     
  8. MarioRuiz

    MarioRuiz

    Joined:
    Nov 7, 2009
    Posts:
    161
    Are there any scripts on your prefab? sounds like an initialization problem. also your prefab collider is a trigger, what does it trigger?
     
  9. Jorjor210

    Jorjor210

    Joined:
    May 3, 2015
    Posts:
    91
    yes there are three scripts on my prefab. and they all get disabled as soon as it spawns. and it triggers the spawn.
     
  10. MarioRuiz

    MarioRuiz

    Joined:
    Nov 7, 2009
    Posts:
    161
    you spawn a prefab that spawns the same prefab on trigger?.. ok

    first, do you disable the scripts anywhere in any of those 3 scripts ?
     
  11. Jorjor210

    Jorjor210

    Joined:
    May 3, 2015
    Posts:
    91
    nope, the only thing that happens is the coin that is triggered gets destroyed. and then another coin is spawned from the same prefab.
     
  12. MarioRuiz

    MarioRuiz

    Joined:
    Nov 7, 2009
    Posts:
    161
    dude would definately need to see the script that does that.
     
  13. Jorjor210

    Jorjor210

    Joined:
    May 3, 2015
    Posts:
    91
    Script that destroys the object:
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. public class Testo : MonoBehaviour {
    4.     public static int Num;
    5.  
    6.     void  OnTriggerEnter2D ( Collider2D info  ){
    7.  
    8.         if (info.name == "Player")
    9.         {
    10.             Debug.Log ("bam");
    11.             Num = Num+1;
    12.             Score.score += +1;
    13.             FinalScore.score1 +=+1;
    14.             Destroy(gameObject);
    15.         }
    16.     }
    17. }
    script that spawns object:
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class randomCoinSpawn: MonoBehaviour
    5. {
    6.     public GameObject prefab;
    7.  
    8.     void OnTriggerEnter2D (Collider2D info)
    9.     {
    10.         if (info.name == "Player")
    11.         {
    12.             SpawnCoin();
    13.         }
    14.     }
    15.    
    16.     void SpawnCoin()
    17.     {
    18.         Vector2 position = new Vector3(Random.Range(-9f, 9F), Random.Range(-5.56F, 5.27F));
    19.         Instantiate (prefab, position, Quaternion.identity);
    20.     }
    21. }
    script that spawns object in beginning (kind of irrelevant but I'm pasting all codes that have anything to do with this prefab) :
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class Spawn : MonoBehaviour {
    5.     public GameObject prefab;
    6.     void Start () {
    7.         Vector3 position = new Vector3 (Random.Range (-9f, 9F), Random.Range (-5.56F, 5.27F), 0);
    8.         Instantiate (prefab, position, Quaternion.identity);
    9.     }
    10. }
    11.  
    that's every single script that deals with this object in any way
     
  14. MarioRuiz

    MarioRuiz

    Joined:
    Nov 7, 2009
    Posts:
    161
    Well I can't see anything wrong at first glance, how many debug.log "bam" are you getting? are you getting any warnings?
     
  15. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    I'm getting a strong sense of deja vu with this thread... what version of unity are you using? did you upgrade from one version to another and have the project "upgrade" to the new version?
     
  16. Jorjor210

    Jorjor210

    Joined:
    May 3, 2015
    Posts:
    91
    I'm only getting one. After that anything I run into gives me absolutely nothing
     
  17. Jorjor210

    Jorjor210

    Joined:
    May 3, 2015
    Posts:
    91
    I'm currently using 5.0.2f1. And I started making this project in 5.0.2f1 but the older version of this game I made (which I'm referencing from) was made in 5.0.1f1