- Home /
Question by
temurginsergeev · Dec 29, 2021 at 03:32 PM ·
c# tutorialclick objectsclickedclicks
when i click it click all objects in scene
Hi!So i do a game where every second spawn object and you need to click it, and my problem is when you click one object it click every object in scene. Here's code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine.UI;
using UnityEngine;
using System;
public class click : MonoBehaviour
{
public static int score;
public Sprite[] newsprite;
public SpriteRenderer spriteRenderer;
[SerializeField]private uint arraySpriteNumber=2;
[SerializeField] private GameObject smokeDestroy;
[SerializeField] private TextMesh scoreText;
private void Start()
{
score = PlayerPrefs.GetInt("score");
scoreText = GameObject.Find("smoketext").GetComponent<TextMesh>();
spriteRenderer = gameObject.GetComponent<SpriteRenderer>();
arraySpriteNumber = 2;
}
public void Update()
{
if (Input.GetMouseButtonDown(0)) isClicked();
scoreText.text = score.ToString();
if (arraySpriteNumber == 0)
{
PlayerPrefs.SetInt("score", score);
PlayerPrefs.Save();
Destroy(smokeDestroy);
}
}
public void isClicked()
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit2D hit = Physics2D.Raycast(ray.origin, ray.direction);
if (hit.collider!=null)
{
score++;
arraySpriteNumber--;
spriteRenderer.sprite = newsprite[arraySpriteNumber];
}
}
}
Comment
Your answer
Follow this Question
Related Questions
Clicking problem 0 Answers
On object clicked open a window using C# 0 Answers
Event System: Check which button has been clicked? 2 Answers
How do I make script in C# the result will execute if all the boxes is fill up? 0 Answers
raycast 2D script issue 1 Answer