Glitch Garden Singleton

Hi Support Team,

I wanna create Singleton with my Defender created, but when completed first scene and moved to second scene, all Defenders has been kept in screen but it didn’t work and have error message.

Please help,



Hi Brian,

Please note, it’s better to copy/paste your code and apply the code fencing characters, rather than using screenshots. Screenshots are ideal for displaying specific details from within a game engine editor or even error messages, but for code, they tend to be less readable, especially on mobile devices which can require extensive zooming and scrolling.

You also prevent those that may offer to help you the ability to copy/paste part of your code back to you with suggestions and/or corrections, meaning that they would need to type a potentially lengthy response. You will often find that people are more likely to respond to your questions if you make it as easy as possible for them to do so.

Have you already compared your code to the Lecture Project Changes which can be found in the Resources of this lecture? And have you already tried to add Debug.Logs to your code to see what is going on during runtime?

Hope this helps :slight_smile:


See also;

Ok sure Nina,

I actually added new method to make the game be funny so that it didn’t relate to Risk’s code.

Here is my code:

public class Shooter : MonoBehaviour
{
    [SerializeField] GameObject bullet, gun;
    GameObject bulletParents;
    const string BULLET_PARENT_NAME = "Bullet Parents";
    [SerializeField] float bulletSpeed;
    AttackerSpawner myLaneSpawner;
    Animator animator;
    bool IsDefenderFromLastScene;

    public void Awake() 
    {
        IsDefenderFromLastScene = FindObjectOfType<Defender>();
    }

    private void Start()
    {
        SetLaneSpawner();
        animator = GetComponent<Animator>();
        CreateBulletParent();
    }

    private void CreateBulletParent()
    {
        bulletParents = GameObject.Find(BULLET_PARENT_NAME);
        if (!bulletParents) bulletParents = new GameObject(BULLET_PARENT_NAME);
    }

    private void SetLaneSpawner()
    {
        AttackerSpawner[] spawners = FindObjectsOfType<AttackerSpawner>();
        foreach(AttackerSpawner spawner in spawners)
        {
            bool IsCloseEnough = (Mathf.Abs(spawner.transform.position.y - transform.position.y) <= Mathf.Epsilon);
            if(IsCloseEnough || IsDefenderFromLastScene) 
            {
                myLaneSpawner = spawner;
            }
        }
    }

    private void Update() 
    {
        Debug.Log(IsAttackerInLand());
        if(IsAttackerInLand() && FindObjectOfType<Lives>().HealthUpdate() > 0 )
        {
            animator.SetBool("isAttacking", true);
        }
        else
        {
            animator.SetBool("isAttacking", false);
        }
    }

    private bool IsAttackerInLand()
    {
        if(myLaneSpawner.transform.childCount <= 0) return false;
        else return true;
    }

    public void Fire()
    {
        GameObject spawnBullet = Instantiate(bullet, gun.transform.position, Quaternion.identity) as GameObject;
        spawnBullet.transform.parent = bulletParents.transform;
        spawnBullet.GetComponent<Rigidbody2D>().velocity = new Vector2(bulletSpeed, 0);
        Destroy(spawnBullet,3f);
    }
}

My SingletonDefenders Code:

public class SingletonDefenders : MonoBehaviour
{
    private void Awake() 
    {
        SetUpSingleton();    
    }

    private void SetUpSingleton()
    {
        if(FindObjectsOfType(GetType()).Length > 1) Destroy(gameObject);
        else DontDestroyOnLoad(gameObject);
    }
}

Thank you. That’s much better. :slight_smile:

What new method did you implement? And what did you expect to happen? What happened instead?

I wanna to keep all my defenders from the current scene to the next scene. I used singleton and all defenders already appeared, but it weren’t shoot, just stand there only

When the defender gets instantiate, SetLaneSpawner gets called to assign a spawner to myLaneSpawner. If the spawner does not get moved to the second level, you will have to call SetLaneSpawner again when the new scene starts to find a new spawner for the defender. Look up the sceneLoaded delegate in the API to learn how to do something when a scene was loaded.

I’ve read and watched some tutorial about sceneLoaded but still not understand Nina. Can you please give me a solution in my current situation?

The solution is to copy and paste the example from the API. Then call SetLaneSpawner in the method that gets called when a new scene was loaded.

ok tks Nina

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms