Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 Forte259 · Apr 04, 2014 at 10:33 AM · functionclassvalueaccessreturn

Access a class on another script?

Hey guys I bump up a problem and I couldnt find an awnswer to ir anywhere so here I am asking for a little help.

So basically I have two scripts, on one I have a class, and on the other I want to create a fuction that will return type of that class of the other script, I've tried many different ways with no luck. I dont know if this is even possible but I apreciatte any help I could get, thank you in advance.

Script 1:

     //Inventory.cs

 public class Inventario : MonoBehaviour {

     public class SlotInv {
 
         public int[] ItemIndex;
         public int[] ItemAmount;
     }
 
         public SlotInv Slot; //Testing
 }

As you can see, here I have my class, and I made a variable from that so I could try to access the class from the other script, with no luck (through my logic)

Script 2:

 //RecipeDataBase.cs

 public class RecipeDataBase: MonoBehaviour {

    public Inventory RecipeItems;
 
     void Start () {
     
         RecipeItems= transform.GetComponent<Inventory> ().Slot;
     }

         //Here's what I want to do (it's wrong but you can get the idea of what I'm trying to achieve)
     public RecipeItems ReturnFinalItemIndex (int finalItemIndex) {
 
         return 0;
     }
 }

This is just one of the many tries I made, this is just to illustrate what I want, so if anyone could help that would be awesome, or just say that is impossible to do. Thank You!

EDITED to give more info

Comment
Add comment · Show 5
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 Benproductions1 · Apr 04, 2014 at 11:29 AM 0
Share

If your one script defined a class called SlotInv, you can't access it through any other name, other than SlotInv. You might want to rename your class to Inventory to match the rest of your code.

Your Script 2 is also not complete, it sort of looks like you're trying to write UnityScript in C#. You can't have variables and functions outside of a class.

For future reference, you should always post the exceptions you are getting so that we can give better responses. Right now I'm literally just guessing why your scripts aren't compiling.

avatar image LijuDeveloper · Apr 04, 2014 at 11:58 AM 0
Share

Try this idea

  private SlotInv  slotScript;

  public string x;

  void Start()
  {
    
    slotScript = GameObject.Find ("Quad").GetComponent<SlotInv>();

    x = slotScript.VariableName ;// accessing variable

    slotScript.FunctionName();// accessing function

  }
 




You can access public class features like

  • public functions

     public void SampleFunction()// This function can access by another script 
    
       {
         //  some code 
       }
    
       void XFunction()// This function cannot access by another script
    
       {
         //  some code 
       }
     
    
     
    
  • public static variables

       public static bool isButtonPress1 ;//  This variable can access by another script
    
         private bool isButtonPress2 ; // This variable cannot access by another script
    
    
         public bool isButtonPress3 ;// This variable cannot access by another script
    
avatar image Forte259 LijuDeveloper · Apr 04, 2014 at 01:40 PM 0
Share

Thank you for your answer, you idea doenst work. I can access variables if I want, my problem is to access a class (in this case inside the monobehaviour class), so a class inside another class, and then make a function that return a type of that class.

I was trying not to use the static variables because they can lead to bugs and at this point I had to change a lot of code...

avatar image Forte259 · Apr 04, 2014 at 12:10 PM 0
Share

Thank you for your comment, first to clarify some things (I should have posted on the question) both scripts inherit from monobehaviour. The only error I'm getting is this one:

Assets/Scripts/Inventory/RecipeDataBase.cs(55,16): error CS0118: RecipeDataBase.RecipeItems' is a field' but a `type' was expected

Even if I change in the Start Function to

 RecipeItems= transform.GetComponent<Inventory> ().SlotInv;

I get the same error

Hope this helps

avatar image fafase · Apr 04, 2014 at 02:57 PM 0
Share

I have seen that co$$anonymous$$g up previously and in the end a class inside works when the wrapping class is not $$anonymous$$onoBehaviour. At least I think it was going this way.

Why are not making your nested class a normal class and use it as you should:

 public class SlotInv {
 
    public int[] ItemIndex;
    public int[] ItemAmount;
 }

 public class Inventario : $$anonymous$$onoBehaviour {
         public SlotInv Slot; //Testing
 }

If Inventario is not $$anonymous$$onoBehaviour then you can nest and use like

 Inventario.SlotInv

but if a class is meant to be used outside its wrapping class then I don't see the point of it. You declare a class inside another class to use it as a container within the wrapper class, if you need to look into that container from somewhere else then make it easy on you and declare it outside.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Ermarrero · Apr 04, 2014 at 02:53 PM

I personally would use a generic list but you can use primitive array..Change to :

 Debug.Log(ReturnFinalItemIndex(2));
 
 public int ReturnFinalItemIndex (int finalItemIndex) 
 {
        //Code for RecipeItems.ItemIndex or RecipeItems.ItemAmount;
     
   return finalItemIndex;
 }
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 Forte259 · Apr 04, 2014 at 06:15 PM 0
Share

that doesnt work for what I'm trying to do, but I've figured it out, and it was super simple that I feel stupid right now lol, I just had to pu my class outside the monobehaviour class and that way I am able to access it the way I want. Thank you guys for all the help.

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

24 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

Related Questions

Return a custom class from function 1 Answer

Return a value for a class? 1 Answer

How can I access a function without knowing the script/class name to which it belongs? 1 Answer

How can I get the value from other scene? 2 Answers

How to Convert a Variable Name to a String? 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