Search Unity

Sending a GameObject variable to another script C#

Discussion in '2D' started by EimantasK, Oct 22, 2018.

  1. EimantasK

    EimantasK

    Joined:
    Oct 19, 2018
    Posts:
    3
    Hello guys, I don't actually have the two scripts to show right now because I'm walking to work now. Although what I and my friend are trying to do is to send a GameObject variable to a different script to be able to know what floor the character is stepping on (there will be 100's of floors so we don't want to hardcode it). We were trying to use GameObject.Find and use the GameObject variable as the string of what we want it to find. Although that doesn't work.


    Thanks in advance guys.


    (I got home here are the scripts)

    CAMERA MOVEMENT:

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;

    public class CameraMovement : MonoBehaviour {

    private Vector2 velocity; //required for calculations

    public float timer_y; //Timer for camera smoothing
    public float timer_x;

    public GameObject player;
    public GameObject floor;

    public float floorMargin; //Margin to adjust y axis on camera

    private PlayerMovement playerMovement;

    void Awake () {
    playerMovement = GetComponent<PlayerMovement> ();
    }

    void Start () {
    player = GameObject.FindGameObjectWithTag ("Player");
    }

    void Update () {
    //floor = GameObject.Find (playerMovement.floorTag.ToString());
    floor = playerMovement.floorTag;
    }

    void FixedUpdate () {

    float position_x = Mathf.SmoothDamp (transform.position.x, player.transform.position.x, ref velocity.x, timer_x);
    float position_y = Mathf.SmoothDamp (transform.position.y, (floor.transform.position.y + floorMargin), ref velocity.y, timer_y);

    transform.position = new Vector3 (position_x, position_y, -1f);

    }

    }




    PLAYER MOVEMENT:

    void OnTriggerEnter2D (Collider2D other) {
    floorTag = other.gameObject;
    }
     
  2. Appleguysnake

    Appleguysnake

    Joined:
    Jan 9, 2015
    Posts:
    19
    You shouldn't be using Gameobject.find in Update() anyway:
    "For performance reasons, it is recommended to not use this function every frame. Instead, cache the result in a member variable at startup. or use GameObject.FindWithTag."

    You don't need to find the gameobject, in your player movement script, you're already setting the floorTag to a field, as long as that field is public, then any other script should be able to reference it whenever they want. Constantly setting it to another field in the camera movement update loop is just duplicating the information. You can either call a method in another script from the player movement OnTriggerEnter2D(), or you can have whatever other function just refer to that public field.
     
  3. EimantasK

    EimantasK

    Joined:
    Oct 19, 2018
    Posts:
    3
    So what you are saying is that if the floorTag is a public variable, the other script will be able to detect it if I write the floorTag variable inside it?
     
  4. KRice

    KRice

    Joined:
    Jun 12, 2018
    Posts:
    7
    Not quite! You need to create an object of the class that holds the variables/functions you want access to in your new class. First, you need to instantiate an object of the first class before you can access it's members; using your code below, that should look like this:

    CameraMovement sampleObj = new CameraMovement();

    Now, in your new class you can reference that object and access the variables and functions from the CameraMovement class. Let's say in your new script you want to make a change to the value of the "timer_x" variable from CameraMovement, that syntax will look like this:

    sampleObj.timer_x += 0.1f;

    If you want to read into Object Oriented Programming in C# a bit more, here is a great place to start: https://docs.microsoft.com/en-us/do.../concepts/object-oriented-programming#Members

    Hope this helps!
     
  5. EimantasK

    EimantasK

    Joined:
    Oct 19, 2018
    Posts:
    3
    We actually found the problem, we did everything correctly except that when sending over the information we didn't mention which GameObject the script is in so it couldn't actually find the script. It's all good now! Thanks for all the help anyways.