Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by montymomentum_unity · Apr 10, 2020 at 07:30 PM · parentchildbooleanchildrenactivate

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;
 
             }
         }
     }
 }
 
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

3 Replies

· Add your reply
  • Sort: 
avatar image
1

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Bunny83 · Apr 11, 2020 at 02:20 AM 0
Share

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.

avatar image
0

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.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image montymomentum_unity · Apr 11, 2020 at 12:57 PM 0
Share

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?

avatar image jkpenner montymomentum_unity · Apr 11, 2020 at 11:15 PM 0
Share

Replace 'PlayerController' with the name of your player component or which ever component your trying to find.

avatar image
0

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

132 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges