- Home /
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.
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?
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.
Your answer
![](https://koobas.hobune.stream/wayback/20220613164522im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
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