- Home /
Text popup on target hit
Hi all, first time posting, new to unity. such a great program. Just wondering if I could ask some help, I have created a mathematical shooting game, and need to understand how to create a popup window of text once a target has been shot. I have everything else just this im not quite getting. I have a ray cast bullet (I have followed Jimmy Vegas FPS script for this) and target disappears once hit, not a problem, however i need to add a script that prompts a popup window with a question, 1+1= is fine at the moment. then user will need to input answer and that will determine if target survives or dies. eventually will need to randomize questions however for the prototype any popup with correct answer should destroy target. any advice or help would be much appreciated, sorry if this is somewhere else in the forums, (I did look but couldnt see), look forward to getting more involved with the community as my scripting skills improve. Thanks.
Answer by MasterChameleonGames · Mar 07, 2019 at 05:14 AM
Have the popup window disabled at the start, and when the target is hit, enable it.
Ideally, you would use an event, but since you're just starting out, an easier way would do.
You can use either script as long as it can detect when the target is hit.
Put all the GameObjects that make up the popup under 1 parent, store a reference to that GameObject in the script, and enable it when the target is shot.
Answer by jonowtpt · Mar 08, 2019 at 12:06 AM
Ok thanks for that, yea kinda makes sense, so just to clarify a few things..
is the popup a 2d canvas with a text script attached for randomized questions?
the event would be 'OnDamage' calling for the target to get damage to action said event?
then would need an if statement to validate answer and perform true outcome?
if i have got this correct do i create separate scripts or do i embed code into existing scripts and reference it?
I think i understand what you are saying i am just unsure of best way to execute it. but i do appreciate your response as it has given me some direction on how to research solutions.
Yes, and you could use the Panel GameObject if you want to make it look more like a popup.
If you want to use an event, the target or bullet script would call the event, and a script on the popup would enable the popup when the event is called. I'd suggest a name like "OnHit".
I'm assu$$anonymous$$g this is about when the player answers the question? Yes, there would need to be an if statement to check if the player is right. If it is free-response, use an input field and call the function on OnEndEdit. Or you could have a submit button. If it is multiple-choice, just use buttons.
You don't have to, you can just store a reference to the popup in an existing script and enable it when you need to ins$$anonymous$$d of using events.
Ok that is awesome! yes i understand exactly what you are saying, i just dont have the scripting skills right now to create such code. are you able to advise of anywhere where someone may be able to assist with the scripting. I dont mean to be so forward and ask directly i am just at the end of my ability level. the below code is my simple enemy health script, and my gun damage script, bullet takes 1 hit points, once enemy health =0 i want to call the popup (1+1=_) with player to submit 2 as correct answer.
var EnemyHealth: int = 1;
function DeductPoints(DamageAmount: int) { EnemyHealth -= DamageAmount; }
function Update() { if (EnemyHealth <= 0) { Destroy(gameObject); } }
var DamageAmount: int = 1; var TargetDistance: float; var AllowedRange: float = 15;
function Update() { if (Input.GetButtonDown("Fire1")) {
var Shot: RaycastHit;
if (Physics.Raycast(transform.position, transform.TransformDirection(Vector3.forward), Shot)) {
TargetDistance = Shot.distance;
if (TargetDistance < AllowedRange) {
Shot.transform.Send$$anonymous$$essage("DeductPoints", DamageAmount);
}
}
}
}
If you are able to advise where/how/what my code should look like regarding the call to pop up i would greatly appreciate it, otherwise you have already been immensely helpful and i thank you for your assistance so far. (i know its a lot to ask). Thanks in advance.
At the top, declare a variable that will store a reference to the popup.
public var popup: GameObject; (you can change the name if you'd like, and you can put [ShowInInspector] ins$$anonymous$$d of public, it will have the same effect)
When you save the code and go back to the Editor, you'll see the variable show up in the Inspector. Drag the popup GameObject over to the box beside the variable's name, and now the code has a reference to it.
Inside the if(EnemyHealth
popup.SetActive(true).
This will make it so that the popup will appear when the enemy is out of health.
Also, if you're looking for tutorials, I'd recommend the official ones, they explain everything the best.