• 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
Question by rgredhcah · Apr 17, 2020 at 09:27 AM · scripting problem

how can i access a instance of a class that is stored inside a script

I have this script that is being attached to my gameObject:

 public class Computer : MonoBehaviour
 {    
     public int AccessLevel;
     public Text text;
     public InputField inp;
     system s = new system();
     // Start is called before the first frame update
     void Start()
     {
         s.link(name, AccessLevel, text, inp);
     }
     public void Run()
     {
         s.Run();
     }
 }

Here is the 'system' script that I'm making an instance of in the script above:

 public class system
 {    
     public string Name;
     public int AccessLevel;
     public Text text;
     public InputField input;
     public system Connected;
     public Dictionary<string, string> memory = new Dictionary<string, string>();
     
     public void link(string n,int al,Text t,InputField i)
     {        
         AccessLevel = al;
         text = t;
         input = i;
     }
 
     public void Run()
     {
         MasterServer.Receive(this);
     }
 }


And finally a 'MasterServer' script that will execute the code:

 public class MasterServer : MonoBehaviour
 {
     public static void Receive(system sender)
     {
         Regex r = new Regex("(connect)\\(\\s*(.*?)\\s*\\);|(.*?)?\\.?(#)\\s*(.*?)\\s*#");
         MatchCollection matches = r.Matches(sender.input.text);
         foreach (Match m in matches)
         {
             List<string> g = new List<string>();
             for (int i = 1; i < m.Groups.Count; i++)
             {
                 if (m.Groups[i].Value != "")
                 {
                     g.Add(m.Groups[i].Value);
                 }
             }
             //==================================================================================
             //Add Commands Here:
             if (g[0] == "connect")
             {
                 sender.Connected = GameObject.Find(g[1]).GetComponent<system>();
             }
             if (sender.Connected.Name == g[0])
             {
                 if (g[1] == "#")
                 {
                     sender.Connected.Print(g[2]);
                 }
             }            
             if (g[0] == "#")
             {
                 sender.Print(g[1]);
             }          
             //==================================================================================
         }
     }
 }

The main problem is this piece of code in the 'MasterServer' script:

 sender.Connected = GameObject.Find(g[1]).GetComponent<system>();

it gives an error: (GetComponent requires that the requested component 'system' derives from MonoBehaviour or Component or is an interface.)

I'm pretty new to unity and can't seem to wrap my head around what to do to fix this.

==========================================================================

The main idea is I want to make a Central server that houses all of my functions. then I create a new system for each computer and link the components to it from the computer. after that when I press a button that is linked to the 'Run()' method in the 'systems' script it gives the 'MasterServer' script the sender and then executes the code from there.

Comment

People who like this

0 Show 0
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
Best Answer

Answer by Jasr_88 · Apr 17, 2020 at 03:08 PM

Hi, well, the component you want to get it's "Computer"

  sender.Connected = GameObject.Find(g[1]).GetComponent<Computer>();

And then you'll need to retrieve the Computer's "system" attribute that you call "s" (but first you need to declare it public in order to acces to it from an other class).

Comment
rgredhcah

People who like this

1 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 rgredhcah · Apr 17, 2020 at 03:45 PM 0
Share

I was not aware that adding a script to an object would make it an independent script, I thought all scripts with the same name would act as one, that was my mistake, I completely removed the 'Computer' script and just used the 'MasterServer' and 'system' scripts, and it works perfectly.

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.

Update about the future of Unity Answers

Unity Answers content will be migrated to a new Community platform and we are aiming to launch a public beta later in June. Please note, we are aiming to set Unity Answers to read-only mode on the 31st of May in order to prepare for the final data migration.

For more information, please read our full announcement.

Follow this Question

Answers Answers and Comments

225 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

Related Questions

Need to vertically space buttons in scripting C# 1 Answer

Raycast Not Working properly 1 Answer

groundCheck in small beginner project not working properly 0 Answers

changing script from key based function to GUI button for dice roll game 1 Answer

Error* Scripting problem.. Dont no how to explain I have script and photo!! 2 Answers


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