Unity Prefab Issue, different reactions from different prefabs - c#

So I've hit a wall in a unity project, and I've been unable to even find a similar problem. I'm not sure how to easily describe it, but here goes:
I have a prefab (a simple 3D object triggered on mouse click/finger tap). It is meant to act as a button (it cannot be a UI button for various reasons). I have a bunch of them. Each button has a script attached, so that when the button is pressed, it will display some text and and some images. However, this is not working.
To test it, I put down a few of these buttons. I filled in the script box for two of them, lets call them A and B. They were directed to the same UI elements, but each was given a different set of images to produce. However, when I test it, it only calls the images from B. If A is the only one set up, all the buttons call from A, even without a script attached.
I need some way to refresh the assigned starting "values" for the variables. So that it will pull from what I assigned them in the editor only when clicked on, and not before. Any help or suggestions would be appreciated.

Try this link Instantiating Prefabs from the unity site.
A possible solution would be to set a global Boolean variable for each button (prefab). If button 1 is active, set the variable to true and in for buttons 2 and 3 make them turn to false for inactive.
I suggest designing your script this way for each button. After the button is pressed refreshed the values in the variable for each button and check these values again once a button is pressed.

You seem to be almost there.
public class A : Mono {
public ImageToShow Image; // Assign this in inspector
void OnMouseDown() {
Image.DoYourThing(); // Do whatever you need to here for the image
}
}
When this is on many different "Prefabs" if you handle the prefabs correctly, each button will have its own instance with its own reference to the image you assigned inside the inspector.
https://docs.unity3d.com/ScriptReference/MonoBehaviour.OnMouseDown.html
Link for OnMouseDown.
Each button can have the SAME script, you just neeed to make sure that each prefab instance you have SAVED has the correct image assigned to its script.
Hope this helps.
Edit: Doing it this way, each button will be "active" but wont do anything if not tapped. If you tap or click an inactive game object (which is your button because its not a GUI element) no method on that object will be called.

Related

Button Command On click() Isn't enabling it's Image

I am trying to do something like X and O game, and I want the image of button be showed if it was pressed but it's not working, I made sure that button is working by reversing the command it worked fine! Here is screenshot
I tried to reverse the command and it worked so the button is working fine
Have in mind that a UI.Button requires at least one active and enabled visual component (Image/Text/etc) with RaycastTarget enabled ... I suspect since you disabled the image you simply disabled the interactions with this button
=> solution could be to simply have two Images like e.g.
- Button (with Button + Image component)
|-- Icon (with Image component)
The Button object is your main button with a background image - can be completely transparent, but can also react to interaction and have color for hover, press etc. This you always leave enabled and here you enable the RaycastTarget.
Then in Icon you assign the according circle or cross sprite and can enable / disable it accordingly. Here you can also deactivate the RaycastTarget to save some performance. You could even have two different child icons according to the sprites then you don't need to assign the sprite on runtime either but just activate according child object

Keymapping in unity

I'm working on a game in unity. I have several things that are supposed to happen when a certain key is pressed, like w to move forward. The problem, is that some people might prefer a different key layout and different buttons for different actions.
I saw lots of unity games where you're able to choose yourself your prefered keys, however I can't seem to find a explanaiton of how to do so.
Is there a way of creating some kind of menu, where when you press on a certain action name, unity then, would record your input, and during the game check if that button is pressed?
You can create your own script what wil handle it.Most easy way to save your keys to json for example and every time game load then this file is loaded.And when user change key binding then you save it to that file.
For example if you want change jump, then you select jump in your menu , let user press button he want and then save button he pressed to that file.So for jump will be used this button.
You can use this
https://docs.unity3d.com/ScriptReference/Event-keyCode.html

How to detect if button was not hit / how to make something fade away if not clicked

Hi i am making a card inventory where i have 4 selected cards and a grid of cards to select from. when pressing a card i have a button that popps up that you can press to select the card, but my problem is that i have no clue how to make that button go away.
i want to have a select button show up when pressing a card, and pressing another will make it go away, or pressing anything else will make it go away. but i have no clue how to.
the dropdown ui template ha this built inn, when clicking the dropdown the options show up, when clicking anything else it goes away, i want that.
edit: for further clarification, i have 2 buttons, 1 makes the other one visable, the other one is going to go away when clicking anything else than the button itself. that is all i have
any idea random wonderful strangers on the internett
love
As I misunderstood your problem the first time, I'll write it the second time.
I think this is really simple to solve, so basically create a huge and transparent button which will detect all of the clicks besides from the cards and buttons.
So my solution is:
Create a button called for example "Background" and set its scale to an enormous values like 1000 to cover the whole screen all time.
Set all of the button's colors (Normal Color, Highlighted, Pressed, etc.) to transparent - basically set the Alpha value to 0
The button has to be above all of the other buttons in the Hierarchy of Canvas (or however you called it) in order to work as a background and not cover up other buttons.
Detect the clicks on the "Background", which will just hide the "Select" and the "Info" button.
I hope this time it will work.

Button seems to animate instead of the others

Here is my UI structure in the Hierarchy:
And here is how it looks:
So basically, when I click the first button, its color changes on click as expected. However when I click the second or the third one, the first one also displays the click animation, instead of the one I clicked.
Here are some things I noticed:
The buttons do register a click as expected.
When animating the button further than the default (by using custom timeline animations) the behavior is slightly different between the first and the second + third ones.
Things to note:
I am using a horizontal layout group on the panel with the following settings:
Each button has a TMP image inside with the same size as the button itself.
I theorize that the HLG is the problem here, but I've tried many settings and the result stayed static.
If you need any additional information, please ask.
Turns out the problem was that the target graphic on the buttons were all set to the first one. That's why they still registered the click properly even though the visuals were messed up.

Why cant I add keyframes to my animator in Unity3D?

I have a Unity3D animation, but, I can't add keyframes, it only gives me the option of Add Animation Event.
I also noticed that in the Animation component of the object, It doesn't allow me to check the box of enabled, If I turn it on, the checkbox becomes red and when I turn on/off the record keyframe mode the checkbox automatically unchecks...
I dont know if it could be related, but, this object was created copypasting of another object that has animations, I just deleted all of them and created new ones, but I dont know if this could have affected the object, because I have 2 objects create the same way, and they allow me to put keyframes.
You should set up your animation like the following:
Select the object you want to animate.
Go to the animation window, click 'Create'.
Save the animation to the desired folder. You should now have an animator file and an animation file.
Be sure the object is selected and add the desired properties on the left and everything should work just fine.
Hope this helps

Categories

Resources