- Home /
Clicking problem on 2D
Hi all, I am new in Unity. I am creating a 2D game that the enemies can be killed by a mouse click. I created Prefab and attached the enemy on it(animator), then I write the following code:
public GameObject magic;
public float spawanrate = 2;
private float nextspawantime;
private GameObject magicclone;
void Start()
{
nextspawantime = Time.time;
}
void Update()
{
if(Time.time > nextspawantime )
{
Vector3 pos = new Vector3(Radom.Range(-8, 8), 10, -1);
magiclone = (GameObject) Instantiate(magic, pos, Quaternion.identity);
nextspawantime = Time.time + spawanrate ;
}
}
Also, I added HitOnEnemy for killing the enemy.
public int mydamage;
void Start()
{
}
void Update()
{
if (Input.GetMouseButtonDown(0))
{
Vector2 pos = new Vector2(Input.mousePosition.x, Input.mousePosition.y);
RaycastHit2D hitInfo = Physics2D.Raycast(Camera.main.ScreenToWorldPoint(pos), Vector2.zero);
if (hitInfo.collider != null)
{
if (hitInfo.transform.gameObject.tag == "Magic")
{
Debug.Log("hit");
MagicState mc = (MagicState)this.GetComponent(typeof(MagicState));
mc.ApplyDamage(mydamage);
}
}
}
}
When I run the game, I got a strange situation is that I click on the enemy (one click only), I got more than 5 "hit" on the console like this:
hit UnityEngine.Debug:Log(Object)
hit UnityEngine.Debug:Log(Object)
hit UnityEngine.Debug:Log(Object)
hit UnityEngine.Debug:Log(Object)
hit UnityEngine.Debug:Log(Object)
Help me please, I have spent a week but could not solve this problem.
------Update------- Thank you everyone who answered my questions. I think, I find the problem which comes from the Floor object (Quad). Because when the Quad is removed, everything is good. So that, I have a work around that I use a Sprite for the background to instead of Quad.
Do you have multiple instances of the script? Add something like +gameObject.name to your Debug.Log
It seems as though each of the prefabs is running the script simultaneously, giving five different instances and therefore five hits (I wouldn't be surprised if you had five of the prefabs). Try adding a statement to check if the object running the script has been hit ins$$anonymous$$d of using tags, so that there is only one hit.
Thanks for the quick reply. Can you please to provide me some sample code for checking? I have no ideas, sorry about that.
What are you testing on? A PC, $$anonymous$$ac or Ipad/Android tablet? I updated my answer. Test it out.
Answer by mohhad · Jul 30, 2014 at 10:30 AM
Try Removing the Hit code from the Update function and add it in a separate OnMouseDown function. Like so:
void OnMouseDown(){
MagicState mc = (MagicState)this.GetComponent(typeof(MagicState));
mc.ApplyDamage(mydamage);
}
Update: I tested your code and debugged it and tried my method. Everything worked fine with single or multiple instances of your character. THe character was taking damage properly and the Hp was being deducted once per character until he disappears. Try this updated file with a clean project. Tested on PC.zip
Thank you so much mohhad. I will try it asap, then tell you the result. I am using PC for development.
Hi mohhad, I tried your clean project that it worked. But I added the floor(Quad) on the scene, the problem is back. So that, I think the problem comes from the floor(Quad). Does the problem relate to the Layer?
Probably a layer problem. Check the 3D view and alter the Z position if needed.
Answer by prashant · Jul 30, 2014 at 10:50 AM
if the collider is attached to the sprite then void OnMouseDown func would get called on mouse click
or
private Collider2D[] cols; if( Input.GetMouseButtonDown(0) { Vector3 clickPosition = Camera.main.ScreenToWorldPoint (Input.mousePosition); int check = Physics2D.OverlapCircleNonAlloc( clickPosition,0.3f, cols);
if( check != 0)
{
cols[0].SendMessage("processTap"); // the first object
}
}
Answer by JoeSzeto · Jul 30, 2014 at 01:03 PM
Thanks for your answers. However, I have tried the answers that provided, it still doesn't work (the code inside onMouseDown doesn't run at all).
Any idealink texts, Thanks a lot again.
I attach part of my project, I think it would be easier to understand.
Answer by 432qwerty · Jul 30, 2014 at 01:43 PM
I think your problem is that in your original code every frame you are checking if the mouse is down, so this would register as multiple "clicks". Using OnMouseDown is the obvious solution but if you are saying this does not work then try adding a one-shot boolean to the code so it can't register the click multiple times. In addition to this you should try attaching the two original scripts to an empty GameObject in your scene which deals with both spawning and click detection remotely so there is no possibility of multiple instances of the scripts running (I presume HitOnEnemy is attached to every prefab when it shouldn't necessarily be). You will have to edit your code a little to enable this.
If the various coding answers have not achieved anything maybe it is worth looking at Unity itself and the properties of your prefabs.
Your answer
Follow this Question
Related Questions
Accessing variable in another script? 2 Answers
Select and save object 1 Answer
use mouse click to pause/stop a transform rotate script on gameobject 1 Answer
C# disable 2 diffirent scripts OnTriggerEnter 1 Answer
2d multiple falling objects 3 Answers