- Home /
Accsesing variables from another script but they have the same name. (C#)
Hi, I'm making a game that involves instantiating lots of rooms randomly according to percentages, with an even number of total doors, and pairing all those doors randomly so when you enter a collider, it teleports you to a different rooms door.
The problem is with duplicate rooms. I'm only testing with one room prefab, and think the problem is that if 2 of the same room gets instantiated, the door script looks to write its location to the static variable in the room script; if they're the same name, it doesn't know which script to write to.
Like I said, I'm using static variables. They seem to be frowned upon, but they are all I know. I've seen people use getcomponent, but that is only useful if i can a door script to communicate with its parent script only.
I have the door coliders and scripts as parents of the room and room script.
I'll post my code just in case It's needed. I only have one room so far that's set up. Warning though, I'm very new to programming and it's not super practical and neat.
One last note would be that when you enter a door right now, it teleports you to the center of the room.
This is my script for creating rooms and storing information. Right now all the public room arrays just have my one 4 door room placed in 3 times.
using UnityEngine;
using System.Collections;
public class floorbuilder : MonoBehaviour {
//floors
public GameObject[] fourrooms = new GameObject[3];
public GameObject[] threerooms = new GameObject[3];
public GameObject[] tworooms = new GameObject[3];
public GameObject[] onerooms = new GameObject[3];
public GameObject[] startrooms = new GameObject[1];
ArrayList currentfloor4 = new ArrayList ();
ArrayList currentfloor3 = new ArrayList ();
ArrayList currentfloor2 = new ArrayList ();
ArrayList currentfloor1 = new ArrayList ();
//data containment
public int floornumb = 1;
float doornumb;
float roomnumb;
float fourdoornumb;
float threedoornumb;
float twodoornumb;
float onedoornumb;
public int Size_Increase_Factor = 3;
public bool enablegeneration = true;
//positional variables
int xnumb = 0;
int znumb = 0;
float snumb = 0;
float rnumb = 0;
//door related things
public static ArrayList doorAport = new ArrayList ();
public static ArrayList doorBport = new ArrayList ();
public static int Aporti = 0;
public static int Bporti = 0;
public static float maxdoornumb;
void Start () {
transform.position = new Vector3 (0, 0, 0);
buildnextfloor ();
}
void buildnextfloor(){
//Math determining the number of rooms and doors relitive to the floor level and shape of room positioning
roomnumb = /*(insert mathy stuff stuff here)*/ (floornumb + floornumb) * Size_Increase_Factor;
fourdoornumb = (Mathf.Round(roomnumb * (Random.Range(0.2f, 0.5f))));
threedoornumb = (Mathf.Round(roomnumb * (Random.Range(0.1f, 0.4f))));
twodoornumb = (Mathf.Round(roomnumb * (Random.Range(0.1f, 0.3f))));
onedoornumb = (Mathf.Round(roomnumb * (Random.Range(0.1f, 0.3f))));
doornumb = (fourdoornumb * 4) + (threedoornumb * 3) + (twodoornumb * 2) + (onedoornumb);
roomnumb = (fourdoornumb) + (threedoornumb) + (twodoornumb) + (onedoornumb);
//if uneven place another door with one room
if (doornumb % 2 == 1) {
onedoornumb ++;
doornumb = (fourdoornumb * 4) + (threedoornumb * 3) + (twodoornumb * 2) + (onedoornumb);
roomnumb = (fourdoornumb) + (threedoornumb) + (twodoornumb) + (onedoornumb);
}
Debug.Log ("Number of rooms: " + roomnumb);
Debug.Log ("Number of doors: " + doornumb);
Debug.Log ("4doors: " + fourdoornumb);
Debug.Log ("3doors: " + threedoornumb);
Debug.Log ("2doors: " + twodoornumb);
Debug.Log ("1door: " + onedoornumb);
maxdoornumb = doornumb / 2;
//Selection of rooms
for (int i = 0; i < fourdoornumb; i++) {
currentfloor4.Add (fourrooms[(Random.Range(0, fourrooms.Length))]);
}
for (int i = 0; i < threedoornumb; i++) {
currentfloor3.Add (threerooms[(Random.Range(0, threerooms.Length))]);
}
for (int i = 0; i < twodoornumb; i++) {
currentfloor2.Add (tworooms[(Random.Range(0, tworooms.Length))]);
}
for (int i = 0; i < onedoornumb; i++) {
currentfloor1.Add (onerooms[(Random.Range(0, onerooms.Length))]);
}
//math for determining the size of square needed
snumb = (Mathf.Round((Mathf.Sqrt (roomnumb))));
rnumb = roomnumb - (snumb * snumb);
snumb = snumb * 100;
rnumb = rnumb * 100;
//placing of rooms
if(enablegeneration){
Instantiate (startrooms[Random.Range(0, startrooms.Length)], transform.position, transform.rotation);
updatelocation ();
for(int i = 0; i < fourdoornumb; i ++){
Instantiate((GameObject)currentfloor4[i], transform.position, transform.rotation);
updatelocation();
}
for(int i = 0; i < threedoornumb; i ++){
Instantiate((GameObject)currentfloor3[i], transform.position, transform.rotation);
updatelocation();
}
for(int i = 0; i < twodoornumb; i ++){
Instantiate((GameObject)currentfloor2[i], transform.position, transform.rotation);
updatelocation();
}
for(int i = 0; i < onedoornumb; i ++){
Instantiate((GameObject)currentfloor1[i], transform.position, transform.rotation);
updatelocation();
}
}
}
//moving the gameobject to make space for next room
void updatelocation(){
if(xnumb < snumb){
xnumb += 100;
}
else if (znumb < snumb){
znumb += 100;
xnumb = 0;
}
else{
znumb += 100;
}
transform.position = new Vector3 (xnumb, 0f, znumb);
}
}
This is My script attached to the room:
int[] doorassignmentnumbers = new int[4];
public static Vector3[] doorloc = new Vector3[4];
bool[] portdefinition = new bool[4];
int doorassigni = 0;
int definitioni = 0;
int leftover = 0;
int transferi;
//door triggers
public static bool d1enter = false;
public static bool d2enter = false;
public static bool d3enter = false;
public static bool d4enter = false;
void Start () {
for (int i = 0; i < 4; i++){
if(floorbuilder.Aporti >= floorbuilder.maxdoornumb){
leftover = (4 - i);
break;
}
floorbuilder.doorAport.Add(doorloc[i]);
doorassignmentnumbers[doorassigni] = floorbuilder.Aporti;
portdefinition[definitioni] = true;
floorbuilder.Aporti ++;
definitioni ++;
doorassigni ++;
}
if (leftover > 0){
for (int i = 0; i < leftover; i++){
floorbuilder.doorBport.Add(doorloc[i]);
doorassignmentnumbers[doorassigni] = floorbuilder.Bporti;
portdefinition[definitioni] = false;
floorbuilder.Bporti ++;
definitioni ++;
doorassigni ++;
}
}
}
void Update(){
if(d1enter){
if(portdefinition[0]){
vp_FPController.playerpos = (Vector3)floorbuilder.doorAport[doorassignmentnumbers[0]];
vp_FPController.teleportf = true;
d1enter = false;
}
else{
vp_FPController.playerpos = (Vector3)floorbuilder.doorBport[doorassignmentnumbers[0]];
vp_FPController.teleportf = true;
d1enter = false;
}
}
else if(d2enter){
if(portdefinition[1]){
vp_FPController.playerpos = (Vector3)floorbuilder.doorAport[doorassignmentnumbers[1]];
vp_FPController.teleportf = true;
d2enter = false;
}
else{
vp_FPController.playerpos = (Vector3)floorbuilder.doorBport[doorassignmentnumbers[1]];
vp_FPController.teleportf = true;
d2enter = false;
}
}
else if(d3enter){
if(portdefinition[2]){
vp_FPController.playerpos = (Vector3)floorbuilder.doorAport[doorassignmentnumbers[2]];
vp_FPController.teleportf = true;
d3enter = false;
}
else{
vp_FPController.playerpos = (Vector3)floorbuilder.doorBport[doorassignmentnumbers[2]];
vp_FPController.teleportf = true;
d3enter = false;
}
}
else if(d4enter){
if(portdefinition[3]){
vp_FPController.playerpos = (Vector3)floorbuilder.doorAport[doorassignmentnumbers[3]];
vp_FPController.teleportf = true;
d4enter = false;
}
else{
vp_FPController.playerpos = (Vector3)floorbuilder.doorBport[doorassignmentnumbers[3]];
vp_FPController.teleportf = true;
d4enter = false;
}
}
}
}
And this is my script attached to each door (the denter variable changer per room as well as doorloc[])
using UnityEngine;
using System.Collections;
public class room_sa_d1 : MonoBehaviour {
void Start(){
room_sa.doorloc [0] = transform.position;
}
void OnTriggerEnter(){
if(vp_FPController.teleport){
room_sa.d1enter = true;
}
}
void OnTriggerExit(){
vp_FPController.teleport = true;
}
}
http://answers.unity3d.com/questions/321385/static-variables.html
Not saying a direct answer or quick-fix for you lies within that topic, but it's a good read and you'll learn stuff.
Your answer
![](https://koobas.hobune.stream/wayback/20220613141941im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
How do i reference a non static singleton? 2 Answers
Logic question, unique path finding system 2 Answers
Will making all variable static reduces the game performance? 2 Answers