Click here to Skip to main content
16,020,990 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
I'm attempting to create a script which allows me to randomly generate platforms infront of my character and the code is:

C#
using UnityEngine;
using System.Collections;

public class SpawnScript : MonoBehaviour
{

    public GameObject[] obj;
    public float spawnMin = 1f;
    public float spawnMax = 2f;

    // Use this for initialization
    void Start()
    {
        Spawn();
    }

    void Spawn()
    {
        Instantiate(obj[Random.Range(0, obj.GetLength(0))], transform.position, Quaternion.identity);
        Invoke("Spawn", Random.Range(spawnMin, spawnMax));
    }

}



This acctually works. But for some reason it only works for a while, then the blocks I'm using turns invisible altough they are still there and I can still run on them. The issue might be that I have it assigned as a child to the camera, but the wierd thing is that it's working for a few seconds and then it proceeds to creating the same blocks but invisible.

It might be another script which is responsible for this? Maybe it's somehow destroying the blocks renderer? If so I can only think of my script which destroys blocks, but it's only supposed to destroy blocks that are behind the Player, since the object that is running the script is attached to my camera and destroys everything it comes in contact with but it's placed in a fixed position behind my player. so it shouldn't remove the renderer on objects infront of it. Maybe it's something wrong in my code?

C#
using UnityEngine;
using System.Collections;

public class DestroyerScript : MonoBehaviour
{

    void OnTriggerEnter2D(Collider2D other)
    {

        if (other.tag == ("Player"))
        {
            Debug.Break();
            return;
        }

        if (other.gameObject.transform.parent)
        {
            Destroy(other.gameObject.transform.parent.gameObject);
        }
        else
        {
            Destroy(other.gameObject);
        }
    }
}


What I have tried:

Well I don't know what is wrong But i have tried finnecking around with my settings but to no avail.
Posted
Updated 10-Mar-16 3:29am
Comments
Per Söderlund 8-Mar-16 18:18pm    
You can edit your questions. No need to delete and recreate.
Pause the editor when the blocks turn invisible. Select a block that is invisible and look in the inspector if everything looks as it should and take it from there.
Member 12376177 8-Mar-16 18:57pm    
Yeah sorry for reposting but I've been stuck here for quite a while now. I checked and there is no difference between the visible and the invisible block. The sprite renderer is on both of them and they have the same scale and rotation.
Per Söderlund 9-Mar-16 4:42am    
Interesting. You have gameobjects with active spriterenderer and sprite that isnt showing. I can't help you from here. Maybe someone else can. I would have to get a copy of your project and run it to help you. Sorry.
Member 12376177 9-Mar-16 6:55am    
Well I played around and I managed to make it a little bit better. Changing the Z axis on the prefabs location to be higher than the cameras Z axis seems to have helped a bit but many blocks are still going invisible.
Per Söderlund 9-Mar-16 16:15pm    
Pause editor and try to make them visible by hand. (moving position, changing parent etc..) cannot think of a better way to find the source of the behaviour.

1 solution

You have invoked spawn within the call of spawn.

Your code goes fully reentrant .. creating objects every one to two seconds infinitely on top of each other ... HENCE THE FLASHING

Look at your hierarchy tab you get ever increasing numbers of spawn

Here is you code step by step

1.) Procedure start calls spawn
2.) spawn Instantiates a copy of a random object from the obj list
3.) Spawn then says to invoke itself (spawn) 1 - 2 seconds later
4.) 1-2 seconds passes spawn is called (AKA goto step 2)

So random objects are being created repeatedly every 1-2 seconds all instantiated on the same point being transform.position AKA the position of the object holding this script.

That what the script says to do and that many objects all ontop of each other will make the screen flash. I suspect you don't want them ontop of each other or at least don't want infinite amount of them which is what happens at the moment.

Lets at least stop the infinity by adding a count which you can set
C#
using UnityEngine;
using System.Collections;
 
public class SpawnScript : MonoBehaviour
{
 
    public GameObject[] obj;
    public float spawnMin = 1f;
    public float spawnMax = 2f;
    public int objCount = 0;   // Yeah we are going to count the objects you make
 
    // Use this for initialization
    void Start()
    {
        Spawn();
    }
 
    void Spawn()
    {
        Instantiate(obj[Random.Range(0, obj.GetLength(0))], transform.position, Quaternion.identity);
        // Only if objCount less than 10 make a new one (stops infinity loop)
        if (objCount < 10) Invoke("Spawn", Random.Range(spawnMin, spawnMax));
        // Increment objCount
        objCount++;
    }
 
}
 
Share this answer
 
v4
Comments
Member 12376177 10-Mar-16 15:18pm    
Unfortunately that didn't change anything. How would i stop them from spawning on eachother? The problem of getting infinetly spawned blocks is stopped by my destroyer script, it destroys everthing it touches witch is everything witch is behind my player. So there's really never very many objects ingame at the same time.
leon de boer 11-Mar-16 4:16am    
Are you sure of screen numbers? your destroyer script only works on collission.

Anyhow why do you instantiate them all at the one point being ===> transform.position

Without really knowing what you are trying to do hard to suggest but I guess count what number spawn you are at and create a new spawn position based on the count something like

Vector3 newSpawnPoint = transform.position; // Copy transform.position to a temp variable
newSpawnPoint.x = objCount * 1.0f; // Now offset x value by 1.0f * objCount (some offset value)
Instantiate(obj[Random.Range(0, obj.GetLength(0))], newSpawnPoint, Quaternion.identity); // Spawn at the new spawn offset position

CAN YOU ALSO EXPLAIN THIS IN DESTROY SCRIPT

if (other.gameObject.transform.parent){
Destroy(other.gameObject.transform.parent.gameObject);
} .....

That basically says if your object has a parent destroy the parent. I don't see where you parent the spawned object object so lost as to why you do it rather than just have the bit inside the else statement

Member 12376177 11-Mar-16 7:31am    
Well I'm very new to C#. What I am trying to do with my spawn script is just having whatever prefabs i have the script on spawn randomly ingame (endless runner kinda thing)

When it comes to the destroyer script, It won't remove the objects unless I have that code there and I don't know as to how I am to change it.
leon de boer 11-Mar-16 22:45pm    
When doing endless spawning the tricky part is to decide where to spawn them because of this issue of spawning them ontop of each other, other objects and the players. On way I may suggest for you is that your spawn point has a trigger collider and only when the spawned monster moves thru the trigger does the invoke to the next spawn start. I assume your spawn script is just sitting on a simple game object point so put a trigger collider on it and an ontrigger code like on the destroyer script.


Try it without destroying parent .. all it should need is this

void OnTriggerEnter2D(Collider2D other){

if (other.tag == ("Player"))
{
Debug.Break();
return;
}

Destroy(other.gameObject);
}
Member 12376177 12-Mar-16 8:18am    
How would I go about creating a trigger for the objects which spawn in my platforms? The objects are always infront of my character (Moving with the camera) So my player never get's close to them, how would I trigger them?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900