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 DJ_ELITE · Apr 28, 2020 at 07:03 PM · accessing from any script

Access UI in scene from an instance prefab

Hi, I'm trying to access a UI element in my scene from an instance but not having much luck. I've managed to not get any errors with this script below but it doesn't SetActive a ui elements or pass variables back. My code on the prefab:-

public class NetworkRoomPlayerLobby : NetworkBehaviour { private Button startGameButton = null; private GameObject gameButton = null; private GameObject lobbyUI = null; private TMP_Text[] playerNameTexts = null; private TMP_Text[] playerReadyTexts = null;

     [SyncVar(hook = nameof(HandleDisplayNameChanged))]
     public string DisplayName = "Loading...";
     [SyncVar(hook = nameof(HandleReadyStatusChanged))]
     public bool IsReady = false;

     private bool isLeader;

     private void Awake()
     {            
         lobbyUI = GameObject.FindGameObjectWithTag("Panel_Lobby");
         playerNameTexts = lobbyUI.GetComponentInChildren<TMP_Text[]>();
         playerReadyTexts = lobbyUI.GetComponentInChildren<TMP_Text[]>();
         playerNameTexts = new TMP_Text[4];
         playerReadyTexts = new TMP_Text[4];
         gameButton = GameObject.FindGameObjectWithTag("StartButton");
         Button startGameButton = gameButton.GetComponentInChildren<Button>();
     }

Here is the code for the UI menu script:-

public class MainMenu : MonoBehaviour { [SerializeField] private NetworkManager networkManager;

     [Header("UI")]
     public GameObject lobbyUI = null;
     public TMP_Text[] playerNameTexts = null;
     public TMP_Text[] playerReadyTexts = null;
     public Button gameButton = null;
     [SerializeField] private GameObject landingPagePanel = null;
     

     public void HostLobby()
     {            
         networkManager.StartHost();            
         landingPagePanel.SetActive(false);            
     }        
 }
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Cassos · Apr 28, 2020 at 07:49 PM

I personally use singletons to find scripts.

 using UnityEngine;
 
 public class FindMe : MonoBehaviour
 {
       public static FindMe singleton;
       void Awake()
       {
               singleton = this;
       }
 }

The object will be found anywhere in the scene if it exists without any performance problems.

 using UnityEngine;
         
 public class TriesToAccessFindMe : MonoBehaviour
 {
        FindMe f;
        void Start()
        {
                f = FindMe.singleton;
         }
 }

The second script shows how you find the script.

Greetings, Max

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 DJ_ELITE · Apr 29, 2020 at 12:57 PM 0
Share

Hi $$anonymous$$ax, Thank you for getting back to me it's much appreciated. I've tried your idea, not getting any errors but doesn't even set the ui to active. I must be implementing it wrong. Here is what I've done. public class $$anonymous$$ain$$anonymous$$enu : $$anonymous$$onoBehaviour { [SerializeField] private Network$$anonymous$$anager network$$anonymous$$anager; public static $$anonymous$$ain$$anonymous$$enu singleton;

     [Header("UI")]
     public GameObject tempLobbyUI = null;
     public T$$anonymous$$P_Text[] tempPlayerNameTexts = null;
     public T$$anonymous$$P_Text[] tempPlayerReadyTexts = null;
     public Button tempGameButton = null;
     [SerializeField] private GameObject landingPagePanel = null;
     public float test = 99f;
     private void Awake()
     {
         singleton = this;
     }

     public void HostLobby()
     {            
         network$$anonymous$$anager.StartHost();            
         landingPagePanel.SetActive(false);            
     }        
 }

In the other script I've put in snippets of the lines in question public class NetworkRoomPlayerLobby : NetworkBehaviour { $$anonymous$$ain$$anonymous$$enu main$$anonymous$$enu;

     [SyncVar(hook = nameof(HandleDisplayNameChanged))]
     public string DisplayName = "Loading...";
     [SyncVar(hook = nameof(HandleReadyStatusChanged))]
     public bool IsReady = false;

     private bool is$$anonymous$$er;        

     private void Start()
     {
         main$$anonymous$$enu = $$anonymous$$ain$$anonymous$$enu.singleton;            
         main$$anonymous$$enu.tempPlayerNameTexts = new T$$anonymous$$P_Text[4];
         main$$anonymous$$enu.tempPlayerReadyTexts = new T$$anonymous$$P_Text[4];            
     }

     public bool Is$$anonymous$$er
     {
         set
         {
             is$$anonymous$$er = value;
             main$$anonymous$$enu.tempGameButton.gameObject.SetActive(true);
         }
     }

public override void OnStartAuthority() { CmdSetDisplayName(PlayerNameInput.DisplayName);
main$$anonymous$$enu.tempLobbyUI.SetActive(true); }

for (int i = 0; i < main$$anonymous$$enu.tempPlayerNameTexts.Length; i++) { main$$anonymous$$enu.tempPlayerNameTexts[i].text = "Waiting For Player..."; main$$anonymous$$enu.tempPlayerReadyTexts[i].text = string.Empty; }

         for (int i = 0; i < Room.RoomPlayers.Count; i++)
         {
             main$$anonymous$$enu.tempPlayerNameTexts[i].text = Room.RoomPlayers[i].DisplayName;
             main$$anonymous$$enu.tempPlayerReadyTexts[i].text = Room.RoomPlayers[i].IsReady ?
                 "<color=green>Ready</color>" :
                 "<color=red>Not Ready</color>";
         }
     }

     public void HandleReadyToStart(bool readyToStart)
     {
         if (!is$$anonymous$$er) { return; }

         main$$anonymous$$enu.tempGameButton.interactable = readyToStart;
     }

If there is anything you can see there why this wouldn't work I'd much appreciate your help again. $$anonymous$$any thanks Chris

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

126 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

Related Questions

Getting Monobehavior script from inspector 0 Answers

all my scripts stopped working after i put a enemy spawner script in 0 Answers

To count deaths with global variable 1 Answer

Access a variable from another script on another object 1 Answer

Check which waypoint is closer 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