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 Liam_Koehler · Apr 02, 2016 at 04:58 AM · c#dataclasseschess

Best way to store and use small game data?

Hi, I have a game that allows me to place rooks on a chess board by clicking on the game squares. I would like to change my game so that I can place either rooks or pawns. I made some UI buttons called "rook" and "pawn". I would like it that when I click the rook button, my game remembers it, and then the after that it places rooks on the board until such as time as when I click the pawn button. Then the game will place pawns etc. My question though, is what is the best way to store this information? Here is some code I wrote, as sort of an experiment:

 using UnityEngine;
 using System.Collections;
 public class GameScript : MonoBehaviour {
     private int piece = 0;
     public int getPiece()
     {
         return piece;
     }
 }  

The code that places the chess pieces (and therefor needs the information of which piece to place):

 using UnityEngine;
 using System.Collections;
 
 public class PlaceChessPieces : MonoBehaviour {
     public GameObject piece;
     private GameScript data;
 
     void Start () {
         Debug.Log("A instance of PlaceChessPieces was initialized");
         piece = Resources.Load("rookPiecePrefab") as GameObject;
         GameScript data = new GameScript();
     }
     void OnMouseDown()
     {
 
         if(data.getPiece() == 0)
         {
             Instantiate(piece, transform.position + new Vector3(0, 0.5f, 0), Quaternion.Euler(-90, 0, 0));
         }
         //Instantiate(piece, transform.position + new Vector3(0,0.5f,0), Quaternion.Euler(-90, 0, 0));
     }
 }

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by meat5000 · Apr 02, 2016 at 11:00 AM

First of all, you should look at the manual for the usage of a "Button".

http://docs.unity3d.com/Manual/script-Button.html https://unity3d.com/learn/tutorials/modules/beginner/ui/ui-button

Once you have digested the information on how to do this the easy way in the editor you can take a look at the scripted method :

http://docs.unity3d.com/ScriptReference/UI.Selectable.OnPointerDown.html

The UI works together with the EventSystem (and input modules) to generate these events and will populate the PointerEventData with the relevant information. If you look at the PED link you will see it has a whole wealth of very useful information including positions and objects pressed. The data even includes a raycast from the Graphics Raycaster (attached to the canvas).

The OnPointerDown link above demonstrates how to set up the Interface. What is doesn't mention is how to use the data.

 public void OnPointerDown (PointerEventData eventData) 
 {
     Debug.Log (eventData.pointerPress.name);
     //pointerPress ->    The GameObject that received the OnPointerDown.
 }

I've simply changed the example in the docs to show the name of the last pressed object generated in the Pointer Event Data.

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

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Unity, Visual Studio and external peripheral 0 Answers

Transfer data between scenes + saving it C# 1 Answer

Most convenient and fast way to persist/access/modify many values from scene to scene? (C#) 0 Answers


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