Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Stopping random spawning on certain tagged objects

Discussion in 'Scripting' started by NeatMaddness, Jul 1, 2015.

  1. NeatMaddness

    NeatMaddness

    Joined:
    Nov 14, 2014
    Posts:
    30
    Ok so I have a script that spawns random "points" in a predetermined area. I have obstacles in this area that I dont want the points to spawn on that have the tag "obstacle." I have an if statement that I thought might be able to stop this but its not working. Any ideas how to accomplish this? Thanks!

    Code (CSharp):
    1. public class Generator : MonoBehaviour
    2. {
    3.    
    4.     public Transform pointDrop;
    5.     public int numToSpawn;
    6.     public Vector3 position;
    7.    
    8.     bool pointSpawned = false;
    9.    
    10.     private float pointTime = 3.0f;
    11.    
    12.    
    13.    
    14.    
    15.     void FixedUpdate()
    16.     {
    17.         int spawned = 0;
    18.    
    19.    
    20.         if(pointSpawned == false)
    21.         {
    22.             if(!gameObject.CompareTag("obstacle"))
    23.             {
    24.                 pointSpawned = true;
    25.                 position = new Vector3(Random.Range(-8.41F, 10.0F), -.46f, Random.Range(5.79F, -3.3F));
    26.                 Instantiate(pointDrop, position, Quaternion.identity);
    27.                 spawned++;
    28.             }
    29.            
    30.         }
    31.        
    32.        
    33.         pointTime -= Time.deltaTime;
    34.        
    35.         if(pointTime <= 0.0f)
    36.         {
    37.             pointSpawned = false;
    38.             ResetTimer();
    39.            
    40.         }
    41.     }
    42.    
    43.    
    44.    
    45.     void ResetTimer()
    46.     {
    47.         pointTime = 3.0f;
    48.     }
    49. }
    50.  
     
  2. WheresMommy

    WheresMommy

    Joined:
    Oct 4, 2012
    Posts:
    890
    Is this script on any obstacle or is it on a kind of gamemanager? In your if statement, you are asking for gameObject, but I cannot find any declaration of this variable. So where do you get gameObject from?
     
  3. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    why are you using FixedUpdate? nothing in that script has anything to do with the physics engine... ?
     
  4. NeatMaddness

    NeatMaddness

    Joined:
    Nov 14, 2014
    Posts:
    30
    ASD, your talking about creating a gameObject var and declaring it in the Awake or Start function? Like obstacle = gameObject.FindObjectWithTag etc. etc. I actually tried that but must have done something wrong in the if statement. Can you provide an example of what would work?

    LeftRighty, it used to have physics stuff in it I just forgot to change it back...

    Would be sweet if someone actually commented with something helping me out.
     
  5. WheresMommy

    WheresMommy

    Joined:
    Oct 4, 2012
    Posts:
    890
    Okay, two suggestions / questions:
    First, how does your scene look like. So where is this script attached to?
    Second, you are using FixedUpdate with this line: int spawned =0; , which says, make spawned = 0 every n-th frame, so your spawned variable will always be reset to 0. What is this var for anyway? Your script just creates it and tries to ++ it, but it is not used anywhere as far as I can see.
    Really wanna help you, but we need more information, where everything is in your scene to be sure, what is going on :) You can also provide a sandbox scene if you want, so I can check it out, because for now, I would not even know, how to test your script.