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 Gilead7 · Feb 23, 2017 at 05:30 PM · c#script.getcomponentaccess

Run a function on a Panel from a script on a prefab programically

I have a prefab with a script attached which handles a mouse click and a trigger enter/exit.

I want to be able to call a function that is on a panel, so it occurred to me to get the script component on the gameobject, but I don't know how to access the functions in the script.

 GameObject main;
     main = gameObject.GetComponent<Maintanence> (this is the name of the script)

Because one script is on a prefab and the other is on a panel, I can't use the "UI way" to hook them together. Ultimately I want to delete an item from the database by clicking on it. Then have the list updated with the current information and instantiated through the prefab. Does that make sense?

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
1
Best Answer

Answer by metalted · Feb 23, 2017 at 08:23 PM

Im not sure if I understand your question. When you run a panel, which is just a UI element, you are probably running it from a gameobject that is in your scene. The prefab will also have to be in the scene if you want to access it.

Running functions in other scripts always seems to be something people can't get their head around. It's actually not that complicated.

Let's say we have a script (class) called Janitor. The Janitor class will have some values (variables) like: His name, his age and maybe his favorite bucket to use. The Janitor can also do things (functions/methods) like: sweeping and whistling.

As a (unity)class this would look like this:

 public class Janitor : MonoBehaviour{
 
     public string name;
     public int age;
     public string favoriteBucket;
 
     public void Sweep()
     {
         Debug.Log("I'm Sweeping");
     }
 
     public void Whistle()
     {
         Debug.Log("I'm Whistling");
     }
 
 }

You could place this class on an object and put it in your scene. But how to make the Janitor sweep the floor?

There are multiple ways to make a script access another script. Lets say we have a school class:

 public class School : MonoBehaviour{
 
 //You could simply make a public variable and drag the Janitor in, in the inspector.
 public Janitor janitor;
 
 //Instead of dragging it in by hand, you could look for the object in the scene:
 void Start(){
 
     //By name, your object must be named "Janitor"  in the scene
     janitor = GameObject.Find("Janitor").GetComponent<Janitor>();
 
     //By tag, your object must have the correct tag (in this case "Janitor").
     //Make sure to have only one object with this tag.
     janitor = GameObject.FindWithTag("Janitor");
 
 }
 }

Now that we found our Janitor class, we can call functions on it:

 janitor.Sweep();
 janitor.Whistle();

So now that we know the basics, lets apply it to your situation:

Let's say your Maintenance class has a function called Repair(). To call this function from another gameobject:

 //Drag in from inspector:
 public Maintenance main;
 
 //Or find the maintenance class in the scene by name:
 Maintenance main = GameObject.Find("MaintenanceObject").GetComponent<Maintenance>();
 
 //Call the function
 main.Repair();

If this is not the answer to your question, i just need some extra information and we'll get it working.

Comment
Add comment · Show 2 · 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 Gilead7 · Feb 24, 2017 at 08:55 PM 0
Share

Thanks for breaking it down for me!

avatar image Gilead7 · Feb 27, 2017 at 07:03 PM 0
Share

It WOR$$anonymous$$ED!

avatar image
0

Answer by SideEffect_ · Feb 25, 2017 at 04:58 AM

What I think you're saying is that in your scene there is a UI panel with an attached MonoBehaviour called maintenance, and a bunch of instantiated UI prefabs, and you want to have them execute a function on Maintenance when clicked. The way I would do this would be to create a static Maintenance variable in the Maintenance class, so any of the prefabs could use a script and access it.

     private Maintenance instance;
     public Maintenance Instance
     {
         get
         {
             if(instance == null)
             {
                 instance = FindObjectOfType<Maintenance>();
             }
             return instance;
         }
     }
 
     private void Awake()
     {
         instance = this;
     }

Then, when you want to execute a method on the Maintenance instance attached to the panel, you could call something like this:

     public void OnPointerUp (PointerEventData eventData) 
     {
         Maintenance.Instance.ExecuteMyLogic(instantiatedPrefabData);
     }
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 Gilead7 · Feb 27, 2017 at 06:40 PM 0
Share

In this particular case, the prefabs show various oil changes taken from an sqlite database. I have triggers that change the text color upon entering or exiting. When clicked, it deletes that entry. I want to be able to run the functions that update the list and display it on screen. Ideally, have a programmatically displayed button to confirm that you want to delete this item.

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

287 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

Related Questions

How to get script from Object with (Clone)'s script? 2 Answers

Multiple Cars not working 1 Answer

How to access the interface from the last component in the inspector 2 Answers

Distribute terrain in zones 3 Answers

Cannot find a script compnent with raycast 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