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. Voting for the Unity Awards are OPEN! We’re looking to celebrate creators across games, industry, film, and many more categories. Cast your vote now for all categories
    Dismiss Notice
  3. Dismiss Notice

Button Problem

Discussion in 'Scripting' started by Cashuz12627, Oct 10, 2018.

  1. Cashuz12627

    Cashuz12627

    Joined:
    Oct 5, 2018
    Posts:
    4
    Hey everyone,

    i have a problem with a game i try to make.

    In the game i build buildings on a map (like in a strategy game). For the buildings i use prefabs. Every building has an menu with a canvas an info text and a button. If you click on the building the menu opens and by clicking the button an action with a timer starts. The button uses a script wich is a component of the mesh.

    Everything works fine for the first building i build. If i build the second one both buttons (action buttons of building one and building two) use the same script.

    Now iam searching for a good way to make the button just use the script of the mesh the button is in?

    Has anyone an idea how to do so?
     
  2. Antistone

    Antistone

    Joined:
    Feb 22, 2014
    Posts:
    2,833
    GetComponentInParent<>() might be useful to you.

    Although generally I try to create links between scripts that need to talk to each other right when they're created so that they don't have to go searching the object hierarchy.
     
  3. Cyber-Dog

    Cyber-Dog

    Joined:
    Sep 12, 2018
    Posts:
    352
    Some code snips would help. Also just checking you have not made any variables or functions Static?
     
  4. Cashuz12627

    Cashuz12627

    Joined:
    Oct 5, 2018
    Posts:
    4
    Code (CSharp):
    1. Public bool menu;
    2. Public float timermax = 5.0f;
    3. Public float timer;
    4.  
    5. Public void Update;
    6.  
    7. If(menu == true)
    8. {
    9. timer = timermax - Time.deltaTime
    10. if(timer <= 0){
    11. timer = timermax;
    12. menu = false;
    13. }
    14. Public void Abbau()
    15. {
    16. menu = true;
    17. }
    Public void Abbau is the method the button calls with Unitys on click method in the inspector. But there are two of this scripts or even more if there are more buildings. And i think the button doesn't know wich one he should take.

    I can give You more code if iam back home.
     
    Last edited: Oct 11, 2018
  5. Cyber-Dog

    Cyber-Dog

    Joined:
    Sep 12, 2018
    Posts:
    352
    Is the script attached to the building? And when you drag an object to the on_click inspector do you drag from the scene or a prefab in your assets folder?
     
  6. Cashuz12627

    Cashuz12627

    Joined:
    Oct 5, 2018
    Posts:
    4
    Yes, the Script is component of the buildings mesh, and i drag it to the on_click from the mesh.
     
  7. Cyber-Dog

    Cyber-Dog

    Joined:
    Sep 12, 2018
    Posts:
    352
    It sound like it should work then, I bet there is gonna be one weird thing that was easily overlooked haha.

    On thing I could think of, I remember years ago hearing something about a freak issue with multiple objects and click events. Maybe you could look at unsubscribing from the event after clicking.
     
  8. Cashuz12627

    Cashuz12627

    Joined:
    Oct 5, 2018
    Posts:
    4
    It works right nie. Spend a few hours with this Yesterday.

    The meshes were all clones of the prefab named "mesh (Clone)". Now every mesh gets a custom name like "mesh1", "mesh2". The Number is an integer from another script that counts +1 for every mesh of this Type i set to the Map.

    After that i deleted the menu in the prefab. Now there ist only one menu in an empty GameObject. If i click on "mesh2" for example, the menu searches for the information of mesh +ID of the mesh i clicked on. This works fine.

    Thanks to everyone for the help.