Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

update GameObject in script 1 from GameObject in script 2

Discussion in 'Scripting' started by c4mino, Aug 31, 2020.

  1. c4mino

    c4mino

    Joined:
    Nov 26, 2019
    Posts:
    10
    Hi all. Total noob here, I have a public field on each GameObject, and I'd like to update one from the other. I am trying to get enableOnComplete (script 1) to turn into enableOnCompleteUpdater (from script 2). Thank you!



    Code (CSharp):
    1.  
    2. //Script 1
    3.  
    4.     public class TextBoxManager : MonoBehaviour
    5.     {
    6.         public GameObject enableOnComplete;
    7.  
    8.         void Start()
    9.         {
    10.         //How do I write the line below?
    11.         enableOnComplete = enableOnCompleteUpdater;
    12.         }
    13.     }
    14.  
    15. //Script 2
    16.  
    17.     public class ActivateTextAtLine : MonoBehaviour
    18.     {
    19.         public GameObject onCompleteEnableUpdater;
    20.  
    21.         void Start()
    22.         {
    23.         }
    24.     }
    25.  
    26.      
     
  2. Dextozz

    Dextozz

    Joined:
    Apr 8, 2018
    Posts:
    488
    enableOnComplete.GetComponent<ActivateTextAtLine>().onCompleteEnableUpdater = // whatever


    You should really learn C# before going into Unity. It will save you a ton of headaches.
    I recommend "C# for absolute beginners" - by Bob Tabor. It should be on MSDN and it's free. That's what I took and he taught me a lot of what I know now.
     
    c4mino and Kurt-Dekker like this.
  3. c4mino

    c4mino

    Joined:
    Nov 26, 2019
    Posts:
    10
    Hey thanks for taking the time to reply! I really appreciate it, and I appreciate the recommendation, I will check it out. I know a very small amount of C# and am actively working to learn more, but I am mostly music/art/animation/sound design. Just trying to learn as I go and I get stuck on a lot of the Unity API. I have almost everything I need for this project, but I still didn't quite get the results I was looking for with the code you posted. I may be using it wrong though. Cheers