Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by stylianos-theodorikakos · Sep 11, 2018 at 04:07 PM · unity 5scripting problemscript.pointspoints-counter

Points-Counter Problem

Hello guys. So I have in my scene 3 canvas. Each canvas has 3 buttons. Each button can give you 0 points, x3 points, x5 points and x10. When you click an object in the scene the canvas gets enabled.Here is the script

 using UnityEngine;
 using UnityEngine.UI;
 using System.Collections;
 
 public class ButtonOnClickActive : MonoBehaviour {
 
     public GameObject canvas;
 
   private bool clicked;
  
  void OnMouseOver()
   {
       if(Input.GetMouseButtonDown(0) && !clicked)
      {
          canvas.SetActive(true);
          clicked = true ;
           GetComponent<Renderer>().material.color = Color.red;
      }             
   }
 }
 

And here is the script for points it is the same for each one just with changed numbers(x3,x5,x10)

 using UnityEngine;
 using System.Collections;
 using UnityEngine.UI;
 
  
 public class x3 : MonoBehaviour {
  
     public Text scoreText;
     public int point;
      
     // Use this for initialization
     void Start () {
    
     }
    
     // Update is called once per frame
     void Update () {
  
         scoreText.text = point + " x3";
  
         if (Input.GetMouseButtonDown(0))
         {
             point++;
         }
     }
 }

Every button has a script accordingly to what points it is going to give. The thing is that when you click the objects, points are added automaticaly adn when you click a button more points are added for every button. Can anyone help me out? Cheers

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by I5 · Sep 11, 2018 at 04:46 PM

Ah, I think I understand. This isn't fully debugged and/or tested, but I think this will get you there. Add this class/behavior to one, and only one, "SceneManager" gameObject that's always active and enabled (can be an empty game object).

 //add this class/component to one, and only one, "master" game object that's always active in the scene, can be an empty game object
         public class SceneManagerClass : MonoBehaviour {
 
             public Camera aCamera;
             [Header("This links game objects in scene to canvases/UI's to be activated")]
             public GameObjectCanvasLink[] goToCanvasLinks;
 
 
             void Start() {
                 foreach (GameObjectCanvasLink goCLink in goToCanvasLinks) {
                     Button[] buttons = goCLink.c.gameObject.GetComponentsInChildren<Button>(true);//assuming that all buttons in the canvas give points
                     for (int i = 0; i < buttons.Length; i++) {
                         Button b = buttons[i];
                         b.onClick.AddListener(delegate { addPoints(b); });
                     }
                 }
             }
 
             public void addPoints(Button b) {
                 //add the points here based on the name of the button 
                 int points = int.Parse(b.gameObject.name.Substring(0, b.gameObject.name.IndexOf('_')));//ex button name "10_MyButton"
                 //do something with the points
                 if (lastActiveCanvas != null) lastActiveCanvas.gameObject.SetActive(false);
             }
 
             private Ray ray;
             private Canvas lastActiveCanvas = null;
             void Update() {
                 if (lastActiveCanvas != null && lastActiveCanvas.gameObject.activeInHierarchy) return;
                 if (Input.GetMouseButtonDown(0)) {
                     ray = aCamera.ScreenPointToRay(Input.mousePosition);
                     RaycastHit[] hits = Physics.RaycastAll(ray, 100f);//change 100 to how far the ray should soot
                     for (int i = 0; i < hits.Length; i++) {
                         foreach (GameObjectCanvasLink goCLink in goToCanvasLinks) {
                             if (hits[i].collider.gameObject.GetInstanceID() == goCLink.go.GetInstanceID()) {
                                 goCLink.c.gameObject.SetActive(true);
                                 lastActiveCanvas = goCLink.c;
                                 break;
                             }
                         }
                         if (lastActiveCanvas != null && lastActiveCanvas.gameObject.activeInHierarchy) break;
                     }
                 }
             }
 
             [Serializable]
             public struct GameObjectCanvasLink {
                 public GameObject go;
                 public Canvas c;
                 public GameObjectCanvasLink(GameObject _go, Canvas _c) {
                     go = _go;
                     c = _c;
                 }
             }
 
         }
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by stylianos-theodorikakos · Sep 11, 2018 at 07:10 PM

thank you for your reply, the thing is to use RaycastAll is a but intermediate to understand and im a beginner. So i tryed to add a point every time the canvas is enabled. Here is the script:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.UI;
 
 
 public class test : MonoBehaviour {
 
     public Text scoreText;
     public int point;
 
     void Update () {
         scoreText.text = point + " x3";
 
         if (gameObject.activeSelf){
             point++;
         }
     }
 }
 

It works good but it doesnt stop unless the canvas gets disabled again. All I need is to add only 1 point. Any thoughts on how to take this parameter?

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image I5 · Sep 11, 2018 at 10:00 PM 0
Share

I don't understand. Why are you adding a point and changing the text every frame in Update, without regard to a mouse click? Can you restate what you're trying to do, maybe with screenshots?

avatar image stylianos-theodorikakos I5 · Sep 11, 2018 at 10:46 PM 0
Share

So here is the button: link text

Then you have 3 different choices (buttons) that one button gives one point to either x5 x3 or x10 link text

And finaly one point gets to the top in the point counter link text

avatar image I5 stylianos-theodorikakos · Sep 12, 2018 at 02:56 PM 0
Share

ok, i think i understand. I updated/edit my original answer with new code. Not fully debugged, tested and/or architected, but I think it'll get you there.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

241 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Non stopping point - counter 1 Answer

The spaceship acceleration script is good ? And how to use it with engine ? 0 Answers

HELP! Scripts looks fine but doesn't respond? 1 Answer

UnityEngine.UI.dll 0 Answers

Double Score Problem ? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges