- Home /
How to make a Boolean unchangeable
I'm making a game where depending on a Button-Press some Objects exist, and others don't. The problem is that some of these Objects are moving platforms so I need to make the Player a Child of the Object for these, and therefore when I deactivate these Objects i also deactivate the Player. Any way to avoid that?
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class Click : MonoBehaviour
{
public GameObject red;
public GameObject blue;
bool CubeExists = true;
void Start()
{
//blue.transform.localScale = new Vector3(0, 0, 0);
blue.SetActive(false);
}
void Update()
{
if (Input.GetMouseButtonDown(0))
{
if (CubeExists)
{
Debug.Log("MouseClick was pressed.");
//blue.transform.localScale = new Vector3(0, 0, 0);
//red.transform.localScale = new Vector3(1, 1, 1);
blue.SetActive(true);
red.SetActive(false);
CubeExists = false;
}
else
{
Debug.Log("MouseClick was double pressed.");
//blue.transform.localScale = new Vector3(1, 1, 1);
//red.transform.localScale = new Vector3(0, 0, 0);
blue.SetActive(false);
red.SetActive(true);
CubeExists = true;
}
}
}
}
Answer by JxWolfe · Apr 11, 2020 at 12:54 AM
Instead of putting the player as a child, simulate that effect in Update() basically using player.transform.x = player.transform.x - oldplat.x + platform.transform.x;
So basically the platform moves, the player moves too. Simulating the player being a child to the platform. Little inefficient, but unless the performance is super top priority, (each milsec counts) then that should be fine.
Right, the question is why you actually want to completely deactivate the platform gameobject. If you do it completely disappears. If you just want to make it stop moving you should just disable the script that moves it.
Answer by jkpenner · Apr 11, 2020 at 03:13 AM
If the player is a child of the object and you have script on the player (maybe called PlayerController). Just before you deactivate the object you could call something like this.
// Check if there is a player attached to the object
var player = objToDeactivate.GetComponentInChildren<PlayerController>();
if (player != null) {
// Detach player from the object
player.transform.SetParent(null);
}
Then in the chance you have more then one player in the scene you could use the GetComponentsInChildren to get an array of all players attached instead of just one.
When I try implementing this I get this error: The type or namespace name 'PlayerController' could not be found (are you missing a using directive or an assembly reference?) Do you know how I could fix it?
Replace 'PlayerController' with the name of your player component or which ever component your trying to find.
Answer by RobeKey · Apr 11, 2020 at 03:15 AM
using UnityEngine;
public class GameManager : MonoBehaviour
{
[SerializeField]
private GameObject player;
[SerializeField]
private GameObject firstPlatform;
[SerializeField]
private GameObject secondPlatform;
private void Awake()
{
SetPlayerAndPlatformsToDefault();
}
private void Update()
{
if (!Input.GetMouseButtonDown(0)) return;
MovePlayerToNextPlatform();
SetNextPlatformAsActive();
}
private void SetPlayerAndPlatformsToDefault()
{
player.transform.parent = firstPlatform.transform;
firstPlatform.SetActive(true);
secondPlatform.SetActive(false);
}
private void MovePlayerToNextPlatform()
{
player.transform.parent = secondPlatform.activeSelf ?
firstPlatform.transform :
secondPlatform.transform;
}
private void SetNextPlatformAsActive()
{
secondPlatform.SetActive(!secondPlatform.activeSelf);
firstPlatform.SetActive(!firstPlatform.activeSelf);
}
}
@montymomentum_unity One way would be to change the player's parent before deactivating the platform they are attached to.
Your answer
Follow this Question
Related Questions
Make a simple tree 1 Answer
Most effective way to get deactivated children? 1 Answer
Target child of child without knowing name? 1 Answer
(C#) How do I run something in my child's script? 2 Answers
all parents of a transform 1 Answer