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 dubiduboni_unity · Mar 12, 2019 at 04:30 PM · c#scripting problemscript.

How can I control each door individual lock/unlock state ?

The first script is attached to a empty GameObject and is working fine, I can control all the doors and change the doors lock/unlock states and changing also the colors of the doors according to the lock states:

 using System.Collections;
 using System.Collections.Generic;
 using System.Linq;
 using UnityEngine;
 
 public class DoorsLockManager : MonoBehaviour
 {
     public bool lockDoors = false;
 
     private Renderer rend;
     private Shader unlitcolor;
     private List<GameObject> DoorShieldFXLocked = new List<GameObject>();
     private List<HoriDoorManager> _doors = new List<HoriDoorManager>();
 
     private void Start()
     {
         DoorShieldFXLocked = GameObject.FindGameObjectsWithTag("DoorShield").ToList();
         unlitcolor = Shader.Find("Unlit/ShieldFX");
 
         var doors = GameObject.FindGameObjectsWithTag("Door");
         foreach (var door in doors)
         {
             _doors.Add(door.GetComponent<HoriDoorManager>());
         }
 
         ChangeDoorsLockStates();
     }
 
     private void Update()
     {
         ChangeDoorsLockStates();
     }
 
     private void ChangeDoorsLockStates()
     {
         for (int i = 0; i < DoorShieldFXLocked.Count; i++)
         {
             if (lockDoors == true)
             {
                 ChangeColors(Color.red, Color.green, i);
             }
             else
             {
                 ChangeColors(Color.red, Color.green, i);
             }
         }
 
         for (int x = 0; x < _doors.Count; x++)
         {
             if (lockDoors == true)
             {
                 LockDoor(x);
             }
             else
             {
                 UnlockDoor(x);
             }
         }
     }
 
     private void ChangeColors(Color32 lockedColor, Color32 unlockedColor, int index)
     {
         var renderer = DoorShieldFXLocked[index].GetComponent<Renderer>();
         renderer.material.shader = Shader.Find("Unlit/ShieldFX");
 
         if (lockDoors == true)
         {
             renderer.material.SetColor("_MainColor", lockedColor);
         }
         else
         {
             renderer.material.SetColor("_MainColor", unlockedColor);
         }
     }
 
     public void LockDoor(int doorIndex)
     {
         _doors[doorIndex].ChangeLockState(true);
     }
     public void UnlockDoor(int doorIndex)
     {
         _doors[doorIndex].ChangeLockState(false);
     }
 }
 

The second script is attached to each door:

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 
 public class HoriDoorManager : MonoBehaviour
 {   
     private List<DoorHori> doors = new List<DoorHori>();
     private bool doorLockState;
 
     private void Start()
     {
         if (transform.parent != null)
         {
             Transform parent = transform.parent;
             var children = parent.GetComponentsInChildren<Transform>();
             
             if(children != null)
             {
                 foreach (Transform door in children)
                 {
                     if (door.name == "Door_Left" || door.name == "Door_Right")
                         doors.Add(door.GetComponent<DoorHori>());
                 }
             }
         }
     }
 
     void OnTriggerEnter()
     {
         if (doorLockState == false)
         {
             if (doors != null)
             {
                for(int i =0; i < doors.Count; i++)
                 {
                     doors[i].OpenDoor();
                 }
             }
         }
     }
 
     public void ChangeLockState(bool lockState)
     {
         doorLockState = lockState;
     }
 }
 

What I want to do is some how to add to the second script the HoriDoorManager script a public bool flag and then in the first script the DoorsLockManager script to create a _doors List but that will contain also the bool flag of each door.

When going back to the editor and running the game I want to see in the inspector of the first script this structure example:

List of the _doors like this:

Door 1 checkbox here Door 2 checkbox here Door 3 checkbox here

So I can control and lock/unlock each door individual. Same like I control all the doors now with the colors change to do it for each door individual.

I'm adding a screenshot image show one of the doors example structure: The first script is attached to empty GameObject the second script is attached to each door Horizontal_Doors_Kit child. And the colors change are of the DoorShieldFXLocked (1) and DoorShieldFXLocked of each door.

The main goal is to be able to control the lock/unlock of each door individual.

Door example

doorscolors.jpg (287.7 kB)
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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by WarmedxMints · Mar 12, 2019 at 04:53 PM

Remove the ChangeLockState Method in the HoriDoorManager script and replace it with this;

     public bool GetLockState
     {
         get { return doorLockState; }
         set { doorLockState = value; }
     }

Change the List of doors to public in the DoorsLockManager script like this;

 public List<HoriDoorManager> Doors = new List<HoriDoorManager>();

Then create a folder called Editor in your Assets folder and then add this DoorsLockManagerEditor.cs script in that folder

 using UnityEditor;
 
 [CustomEditor(typeof(DoorsLockManager))]
 public class DoorsLockManagerEditor : Editor
 {
     DoorsLockManager _manager;
 
 
     public override void OnInspectorGUI()
     {
         _manager = (DoorsLockManager)target;
 
         base.OnInspectorGUI();
 
         if (_manager.Doors.Count <= 0)
             return;

         for(var i = 0; i < _manager.Doors.Count; i++)
         {
             if (_manager.Doors[i] == null)
                 continue;
 
             _manager.Doors[i].GetLockState = EditorGUILayout.Toggle("Door " + i + " Lockstate", _manager.Doors[i].GetLockState);
         }
     }
 }


You will then be able to see a checkbox for each door in your manager script.

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
avatar image
0

Answer by ForbiddenSoul · Mar 12, 2019 at 05:24 PM

I am not sure if you just want to use one script or both.

You could make a Door class, then attach that script to everything that is a door. You can then set the individual properties of each door on their own. Lockstate, a lock state custom color, or anything else you might want to add. Once you made the class you could add a Dictionary to your manager class that has indexed all of the doors. You can the look up the door in your dictionary to get a hold of a specific or multiple doors.

If you structure the way you access the .transform of your doors, all you really need is a door class attached to the GameObject and won't need a dictionary. Something wants to change the lock state of a door, that thing should know what the door is, and you change it.

"ChangeDoorsLockStates()" every update seems unnecessary. Simply change the lock state of one door, when that one door changes, not every update.

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

618 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 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 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 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 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

How can i pick two randomly items from gameobject array ? 1 Answer

Script for a hue rainbow scroll on the material 1 Answer

How can i using a break point if a gameobject have a collider after added to it ? 1 Answer

How can i get all childs from List ? 3 Answers

How can i set the same script on two GameObjects so the script will work on both objects same time ? 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