Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
This post has been wikified, any user with enough reputation can edit it.
avatar image
0
Question by GhostDre · Oct 06, 2014 at 05:37 PM · c#password

Bring class variable into other class

Hi everyone, Im stuck on how to implement this variable from one class to another in my code. I want to have my Combination from my Keypad class equal the password from my Item class. The reason why I want to do this is because this password you find within the game will be the combination you need to put in the keypad. Heres the script:

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 
 public class RayCasting : MonoBehaviour
 {
     public float pickupDistance;
     public List<Item> items;
     public List<Keybutton> buttons;
 
     #region Unity
     void Start ()
     {
         Screen.lockCursor = true;
 
         for (int i = 0; i < items.Count; i++) {
             Item temp = items[i];
             int randomIndex = Random.Range(i, items.Count);
             items[i] = items[randomIndex];
             items[randomIndex] = temp;
         }
     }
     void Update ()
     {
         RaycastHit hit;
         Ray ray = new Ray(transform.position, transform.forward);
         if (Physics.Raycast(ray, out hit, pickupDistance))
         {
             foreach(Item item in items)
             {
 
                 if(Input.GetMouseButtonDown(0)) {
                     if (item.gameObject.Equals(hit.collider.gameObject))
                 {
                     numItemsCollected++;
                     item.Collect();
                     break;
                         }
                 }
             }
 
 
 // This is for the KeyPad Buttons Area
 
 
             foreach(Keybutton button in buttons)
             {
                 
                 if(Input.GetMouseButtonDown(0)) {
                     if (button.gameObject.Equals(hit.collider.gameObject))
                     {
  hit.transform.renderer.material.color = Color.red;
                                   button.ButtonWasClicked(button.currentCombination);
                         break;
                     }
                 }
             }
 
         }
     }
     

Here is where the issue may start. This class is the Item class which calls in the Collect variables and password.

 [System.Serializable]
 public class Item
 {
     public string name;
     public GameObject gameObject;
     public int password;
     
     public bool Collected { get; private set; }
 
 
     
     public void Collect()
     {
         Collected = true;
         //gameObject.SetActive(false);
     }
 
     public void passwordNumber()
     {
         password = 0;
         Collected = true;
         gameObject.SetActive(false);
     }
 
 
 }


This here could be another issue, as this is the Keybutton class. This is where the gameObject button are assigned numbers 0-9 through the currentCombination. The thing is that we want the Combination to equal the password we find in the Item class. Once we find the password, we go put in the currentCombination, and if its correct Combination it should say correct passcode, other wise wrong passcode.

 [System.Serializable]
 public class Keybutton
 {
     public GameObject gameObject;
     //public int Numberbutton;
     private int Combination;
     public int currentCombination;
     public int buttonClickProgress;
     
     public void  ButtonWasClicked (int buttonNmb){
         Item item = new Item ();
 
         Combination = item.password;
         buttonClickProgress+=0;
         currentCombination = buttonNmb;
         
         if(buttonClickProgress < 10){
             decimal d = (decimal)currentCombination / 10;
             Debug.Log(currentCombination);
         }
         else{
             if(currentCombination == Combination){
                 Debug.Log("Correct Passcode");
                 buttonClickProgress = 0;
                 currentCombination = 0;
             }
             else{
                 Debug.Log("Wrong Passcode, reseting...");
                 buttonClickProgress = 0;
                 currentCombination = 0;
 
             }
         }
     }   
 }

So to recap I was wondering how to get the Combination variable to equal the passcode, so once we put in the currentCombination it should equal the Combination which is equal to the passcode you find.

Comment
Add comment · Show 1
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 KayelGee · Oct 07, 2014 at 11:55 AM 0
Share

I'm not sure if I understand you so I'll try to reformulate your problem:

You have a keypad through which you input a combination. The keys of your keypad are represented through your $$anonymous$$eybutton class. When a button gets pressed, it adds it's number to the combination. When you have typed in 9 numbers, the combination gets checked against a passcode. The passcode comes from an item which you can pick up.

Now you want to know how you can access the passcode inside the item from inside the $$anonymous$$eybutton class.

Is that correct?

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by unimechanic · Oct 07, 2014 at 04:02 PM

how to implement this variable from one class to another in my code

You are already doing it:

  public void  ButtonWasClicked (int buttonNmb){
         Item item = new Item ();
  
         Combination = item.password;

But it seems you are trying to achieve it by brute force, since you are creating a new item and assigning its blank value to the combination, and then you don't actually use it. Looking at your other questions you are simply making code and mixing it together, expecting it will work:

http://answers.unity3d.com/questions/795509/remote-keypad-with-buttons.html http://answers.unity3d.com/questions/803110/combination-code.html

So you need to isolate each problem, solve it and understand how it works. Then you can bring the solution to your main project, not by copy-pasting, but re-writing what you understood. For example, if you need to understand how to detect an object with Raycast, do only that in a new project. If you need to understand how to register a sequence of buttons being pressed, simulate the touches with something simple like GUI buttons, and focus on building the sequence in a string. Then create a new test to get the password string from the class that receives that input, into the manager class that uses it to control the game logic. Emulate everything that is not part of the small problem you are trying to solve.

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

Remote KeyPad with Buttons 1 Answer

How do you make a password script in c#? 0 Answers

Constant Rotation in 2D 0 Answers

Using c# with javascript? 0 Answers

Make Health script lose health when certain animation is played? 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