Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Access variable script

Discussion in 'Scripting' started by iRSS, Apr 4, 2020.

  1. iRSS

    iRSS

    Joined:
    Mar 27, 2020
    Posts:
    9
    Hi,

    I'm new on Unity and I'm making a game for test what I'm learning.

    I made 2 objects City1 and City2, each one have one script like that:

    Code (CSharp):
    1. #region variables
    2. [Header("Map settings")]
    3. public string mapName;
    4. [Space]
    5.  
    6. [Header("Camera position")]
    7. public Vector2 maxPosition;
    8. public Vector2 minPosition;
    9. [Space]
    10.  
    11. [Header("Entrance Vector")]
    12. public Vector2 north;
    13. public Vector2 south;
    14. public Vector2 west;
    15. public Vector2 east;
    16. #endregion
    And I created one script for control the teleport:

    Code (CSharp):
    1. #region Enum
    2. public enum Direction
    3. {
    4.     North, South, West, East
    5. }
    6. #endregion
    7.  
    8. public class TeleportController : MonoBehaviour
    9. {
    10.     #region Variables
    11.     public GameObject teleportScript;
    12.     public Direction direction;
    13.     private GameObject teleport;
    14.     #endregion
    15. }
    With this script I wanted to choose on teleport the script of my map, that gave the Vector to move my player.
    But I can't found a way to access the script without put the name of the script attached.

    I tried to do like:
    Code (CSharp):
    1. teleport = teleportScript.GetComponent<teleportScript.name>();
    But I'm getting Namespace can't be found.

    Thanks in advance.
     
  2. ShamusO

    ShamusO

    Joined:
    Jan 12, 2016
    Posts:
    6
    Hey so, to be honest, I'm pretty new to scripting in Unity and C# but I think I may have some help. So it seems the goal is to teleport the player to different regions of the map. I am not sure how you are attempting to implement this based on what you posted (though I may not be well versed enough) are you having the player choose a location from the map? Are you having the player character walk into a teleporter object? So if you could please expand on how this is supposed to work.
     
  3. iRSS

    iRSS

    Joined:
    Mar 27, 2020
    Posts:
    9

    I wanted to drop the script of the map on teleport script and choose the direction.
    Then pick inside the script the values to teleport.
     
  4. kasztelan

    kasztelan

    Joined:
    Nov 3, 2014
    Posts:
    11
    I don't think I fully understand your setup

    What's the class name? Is it extending MonoBehaviour? Ok lets call this class XClassX

    I don't understand what these two are and what they're supposed to do. If they're instances of some specific MonoBehaviour you should explicitly use its name instead of generic GameObject

    Unfortunately you can't do it like that, first the <> takes a type of value not value itself (I'm assuming name is a string), second it needs to be known at compile time so you can't use variable here.

    Assuming you wanted to get the XClassX you should use

    Code (CSharp):
    1. .GetComponent<XClassX>();
     
    iRSS likes this.
  5. iRSS

    iRSS

    Joined:
    Mar 27, 2020
    Posts:
    9
    That part explained what I wanted to do. I will need to modify what I was doing, since I expected a variable inside the <>.
    Thanks for the help.
     
  6. Michelle_Ca

    Michelle_Ca

    Joined:
    Aug 19, 2019
    Posts:
    113
    I was looking for the same thing and i found the solution in Inheritance & Polymorphism
    Where you can call just the parent script name, and this will lead you to the child script of each city without the need to know their names
     
  7. iRSS

    iRSS

    Joined:
    Mar 27, 2020
    Posts:
    9
    I tried this too, maybe I tried on wrong way.