Model as Prefab - lots of this prefab causing problems
Hi there!
I have a problem. I have made a Keypad/Keylock Model with GUI in WorldSpace and Script on it. I have made a prefab with this Gameobject/Model. If i use one of this prefab in the scene everything works fine. but if i use this prefab more than 1 time in the scene i get problems. because the second prefab is talking with the first prefab. if i press a button on the second prefab, the button get pressed on the first prefab and on the second prefab nothing happens! why is this so? i show you a pic of the inspector variables!
public class Keypad : MonoBehaviour
{
public string correctPassword;
private string passwordInput;
public Door affectedDoor;
private int numberOne;
private int numberTwo;
private int numberThree;
private int numberFour;
public Text numberOneText;
public Text numberTwoText;
public Text numberThreeText;
public Text numberFourText;
private bool numberOneAdded = false;
private bool numberTwoAdded = false;
private bool numberThreeAdded = false;
private bool numberFourAdded = false;
private bool keypadActive = false;
public Text passwordField;
public Camera mainCam;
public Camera keypadCam;
public MeshCollider meshCol;
private FirstPersonController playerController;
private GameManager_ToggleCursor toggleCursor;
void Start ()
{
SetInitialReferences ();
}
void SetInitialReferences ()
{
playerController = GameObject.FindObjectOfType<FirstPersonController> ();
toggleCursor = GameObject.FindObjectOfType<GameManager_ToggleCursor> ();
}
void Update ()
{
if ( Input.GetKeyDown (KeyCode.Q) )
{
LeaveKeypad ();
}
}
public void MoveCameraToKeypad()
{
this.mainCam.enabled = false;
playerController.enabled = false;
this.gameObject.layer = LayerMask.NameToLayer ("Default");
toggleCursor.isCursorlocked = false;
Cursor.visible = true;
this.keypadActive = true;
this.keypadCam.enabled = true;
this.meshCol.enabled = false;
}
public void LeaveKeypad ()
{
this.mainCam.enabled = true;
playerController.enabled = true;
toggleCursor.isCursorlocked = true;
Cursor.visible = false;
this.keypadCam.enabled = false;
this.meshCol.enabled = true;
if (keypadActive == false)
{
this.gameObject.layer = LayerMask.NameToLayer ("Default");
}
else
{
this.gameObject.layer = LayerMask.NameToLayer ("Interactable");
}
}
public void CountNumberOneUp ()
{
numberOne += 1;
numberOneText.text = numberOne.ToString ();
if(numberOne > 9 )
{
numberOne = 0;
numberOneText.text = numberOne.ToString ();
}
}
public void CountNumberTwoUp ()
{
numberTwo += 1;
numberTwoText.text = numberTwo.ToString ();
if(numberTwo > 9 )
{
numberTwo = 0;
numberTwoText.text = numberTwo.ToString ();
}
}
public void CountNumberThreeUp ()
{
numberThree += 1;
numberThreeText.text = numberThree.ToString ();
if(numberThree > 9 )
{
numberThree = 0;
numberThreeText.text = numberThree.ToString ();
}
}
public void CountNumberFourUp ()
{
numberFour += 1;
numberFourText.text = numberFour.ToString ();
if(numberFour > 9 )
{
numberFour = 0;
numberFourText.text = numberFour.ToString ();
}
} //Count numbers down
public void CountNumberOneDown ()
{
numberOne -= 1;
numberOneText.text = numberOne.ToString ();
if ( numberOne < 0 )
{
numberOne = 9;
numberOneText.text = numberOne.ToString ();
}
}
public void CountNumberTwoDown ()
{
numberTwo -= 1;
numberTwoText.text = numberTwo.ToString ();
if ( numberTwo < 0 )
{
numberTwo = 9;
numberTwoText.text = numberTwo.ToString ();
}
}
public void CountNumberThreeDown ()
{
numberThree -= 1;
numberThreeText.text = numberThree.ToString ();
if ( numberThree < 0 )
{
numberThree = 9;
numberThreeText.text = numberThree.ToString ();
}
}
public void CountNumberFourDown ()
{
numberFour -= 1;
numberFourText.text = numberFour.ToString ();
if ( numberFour < 0 )
{
numberFour = 9;
numberFourText.text = numberFour.ToString ();
}
}
//Add numbers to the password field
public void AddNumberOne ()
{
if(numberOneAdded == false )
{
passwordField.text = passwordInput += numberOne.ToString ();
numberOneAdded = true;
}
}
public void AddNumberTwo ()
{
if ( numberTwoAdded == false && numberOneAdded == true )
{
passwordField.text = passwordInput += numberTwo.ToString ();
numberTwoAdded = true;
}
}
public void AddNumberThree ()
{
if ( numberThreeAdded == false && numberTwoAdded == true && numberOneAdded == true )
{
passwordField.text = passwordInput += numberThree.ToString ();
numberThreeAdded = true;
}
}
public void AddNumberFour ()
{
if ( numberFourAdded == false && numberThreeAdded == true && numberTwoAdded == true && numberOneAdded == true )
{
passwordField.text = passwordInput += numberFour.ToString ();
numberFourAdded = true;
}
}
public void ResetNumbers ()
{
passwordInput = "";
passwordField.text = "";
numberOneAdded = false;
numberTwoAdded = false;
numberThreeAdded = false;
numberFourAdded = false;
}
public void CheckIfPasswordIsCorrect ()
{
if(passwordInput == correctPassword )
{
passwordField.text = "Access granted";
numberOneAdded = true;
numberTwoAdded = true;
numberThreeAdded = true;
numberFourAdded = true;
keypadActive = false;
if(affectedDoor.lockedWithKeypad == true )
{
affectedDoor.isDoorLocked = false;
}
StartCoroutine (WaitToLeaveAutomatic ());
}
else
{
passwordField.text = "Access denied";
passwordInput = "";
numberOneAdded = false;
numberTwoAdded = false;
numberThreeAdded = false;
numberFourAdded = false;
}
}
IEnumerator WaitToLeaveAutomatic ()
{
yield return new WaitForSeconds (2);
LeaveKeypad ();
}
}
why is this so? thanks for help!
Could you include your $$anonymous$$eypad script, please? That would be helpful.
i dont know why the prefab dont work by itself. my problem is that the 2 prefabs share the same eventcamera set in the canvas. but the camera jumps everytime to the prefab on the right (i placed the second prefab on the left from the first). because of this i see the left prefab on the screen but the input events happen on the other one. if i have only one prefab of this in the scene everything works correctly! i have no idea why always the camera on the original prefab will be activated?!
@strudi1986 please don't post your code as an answer. Just edit your original question and add the code, which I've done for you this time. But please delete your "answer", it's confusing.
Your answer
Follow this Question
Related Questions
Instances of the same prefab behaving differently 0 Answers
How to safely delete a prefab? 0 Answers
Linking a Prefab as Variant to Another Prefab After Creation 0 Answers
Communicate between scenes with prefabs? 3 Answers
Prefabs floating in the air 0 Answers