Skip to main content

Indie game storeFree gamesFun gamesHorror games
Game developmentAssetsComics
SalesBundles
Jobs
Tags

Help me please with my Code (:

A topic by GreenViper2020 created Feb 21, 2022 Views: 267 Replies: 20
Viewing posts 1 to 11
Submitted (2 edits)

So I made a code to activate and deactivate 2Game Objects. Im programing since a month and i have no Idea whats wrong with the code. Please Awnser me. The first Time i press it it works but the second time it doesnt't. And after that it still doesnt works and so on.

This is the code:

using System.Collections;

using System.Collections.Generic;

using UnityEngine;

public class Mapswitcher : MonoBehaviour

{

    public GameObject map1;

    bool map1active;

    public GameObject map2;

    bool map2active;

    // Start is called before the first frame update

    void Awake()

    {

         map1active = true;

         map1.SetActive(true);

         map2active = false;

         map2.SetActive(false);

    }

    // Update is called once per frame

    void Update()

    {

        if (Input.GetButtonDown("Mapswitch") && map2active == false && map1active == true)

        {

            map1.SetActive(false);

            map2.SetActive(true);

        }

        else if (Input.GetButtonDown("Mapswitch") && map2active == true && map1active == false)

        {

            map1.SetActive(true);

            map2.SetActive(false);

        }

        

    }

}

 

where is the problem your geting in?

I tried your code and it works what is the problem and what are you trying to do, to help you better I say

Submitted

I'm trying to build a game that has 2realitys one has real things and one has fake thinks and you have to switch between them to get to the end. But when i hit the button the switch between them it only works the first time after that  map1 stays activadet even when i press the button agein.

many of us have already solved your problem, choose the one that suits you best, but a teacher taught me how to deal with conditionals that can be a bit #~@| so try to keep them as simplified as possible at the beginning so that you don't get headaches, I'm just saying

ho i see :)

Submitted

It seems that you forgot to update the mapActive bools when you switch your map.

Submitted (1 edit)

Do this for the if statements

if (Input.GetButtonDown("Mapswitch") && map2active == false && map1active == true)

        {

            map1.SetActive(false);

            map2.SetActive(true);


         //new code

         map1active = false;

         map2active = true;


        }

        else if (Input.GetButtonDown("Mapswitch") && map2active == true && map1active == false)

        {

            map1.SetActive(true);

            map2.SetActive(false);

         //new code

         map1active = true;

         map2active = false;


         


        }

Submitted

Thank you :D

Submitted

Any time :)

Use this

 public GameObject map1;

    bool map1active;

    public GameObject map2;

    bool map2active;

    // Start is called before the first frame update

    void Awake()

    {

        map1active = true;

        map1.SetActive(true);

        

        map2active = false;

        map2.SetActive(false);

    }

    // Update is called once per frame

    void Update()

    {

        if (Input.GetButtonDown("Jump"))

        {

            map1active = !map1active;

            map2active = !map2active;

            ChangeMap();

        }

    }

    void ChangeMap()

    {

        map1.SetActive(map1active);

        map2.SetActive(map2active);

    }

Change your button for the one I use :)

Submitted

Thank you :D

I don't know what your traying to do but.. try

this:

        if (Input.GetButtonDown("Mapswitch") && map2active == false && map1active == true)

        {

            map1.SetActive(false);

            map2.SetActive(true);

             map1active == false;

             map2active == true; 

       }

        else if (Input.GetButtonDown("Mapswitch") && map2active == true && map1active == false)

        {

            map1.SetActive(true);

            map2.SetActive(false);

             map1active == true;

             map2active == false; 

        }

Submitted

Thanks :D

To me? i was thinking a lot.. for your code

I don't know what your traying to do but.. try

this:

        if (Input.GetButtonDown("Mapswitch") && map2active == false && map1active == true)

        {

            map1.SetActive(false);

            map2.SetActive(true);

             map1active == false;

             map2active == true; 

       }

        else if (Input.GetButtonDown("Mapswitch") && map2active == true && map1active == false)

        {

            map1.SetActive(true);

            map2.SetActive(false);

             map1active == true;

             map2active == false; 

        }

From my experience, the more flexible the code, the less chance there is that an error will appear, according to me :)
Submitted

This code switches between maps, just switch inputs as you wish. I used (InputGetKeyDown).

using System.Collections;

using System.Collections.Generic;

using UnityEngine;

public class Mapswitcher : MonoBehaviour {

public GameObject map1, map2;

// Start is called before the first frame update
void Awake()
{
     map1.SetActive(true);
     map2.SetActive(false);
}

// Update is called once per frame
void Update()
{
    // Input.GetButtonDown("Mapswitch")

    if (Input.GetKeyDown("e"))
    {
        map1.SetActive(!map1.activeInHierarchy);
        map2.SetActive(!map2.activeInHierarchy);
    }
}

}

(+1)
I agree with him,I agree with him, much more simplified and functional
Submitted(+2)

Thank you very much :D