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*;*
-
int randomIndex = Random.Range(i, items.Count);*
_ items = 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.