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 /
avatar image
0
Question by W1k3 · Apr 04, 2014 at 09:34 PM · c#staticlogiccommunication

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;
     }
 }
 






 
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 Lo0NuhtiK · Apr 04, 2014 at 09:51 PM 0
Share

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.

0 Replies

· Add your reply
  • Sort: 

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

22 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


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