how do i access another scripts object that is instantiated?

so my question is how do i Acess script A’s canvas in Script B and script a is attached to a Canvas while Script b is not attached to gameobjects.

normally i would go

private Class somename;

public Player (){

somename.GetComponent<class> ();
or
???

}

but to my understanding that graps the component on the script ?

so how do i grap another components script and use it?

also when i try to do it i get the gameobject not set to a instance.

oh and i also have two scenes one where the UI is and one where the player is

Either You have to use public variable and drag object in the inspector or You have to find gameobject You want to put into variable.

So it would look like so:

public GameObject a;

If You want to place it in the inspector manually.

GameObject a;
aObjectScript aScript;

void Start(){
//Find object
a = GameObject.Find("a");
//Get script from it
aScript = a.GetComponent<aObjectScript>();
}

Then if objcet script have public variable You can just access it like so:

aScript.variable = 1;
1 Like
public GameObject OtherThing;
public BoxCollider SomeBoxCollider;

public void Start()
{
    SomeBoxCollider = OtherThing.GetComponent<BoxCollider>();
}

You have to have a reference to the thing that holds what you want. If you instantiate it at runtime then just store that reference and use it to get the components you want.

You can use GameObject.Find, but this is very bad practice. Store your needed instantiated references properly or assign your predefined gameobjects in the inspector beforehand.

some more info class A uses monobehaviour while Class B doesn’t.

also i prefer not to use inspector way. however the class A has a couple of public fields. but they seem to be lost when i change scene

If Class B is not a Monobehavior then it has to have a host class which is a Monobehavior or a static instance. Access is the same at that point, get the reference, call GetComponent on that reference.

does all classes in unity need to have a monobehaviour at some point?

You’re probably looking at two difference instance in the two different scenes, I would guess? Tough to say from the brief description of what you’re seeing here.

In your initial post describing your problem, you’re sort of all over the place and I can’t tell what you’re trying to say exactly. This matters because the exact situation you’re in determines the best way to get a reference to the other GameObject. There are probably a good dozen ways to do it, for different situations (and GameObject.Find should NOT be one of them, by the way; there is always, always a better way than GameObject.Find.)

In the case of UI, a singleton pattern is often a good way to get the needed reference.

No they do not and here is how You can do it. I just misread what You are looking for in my first answer.

public static class Bla{
public static Bla current = new Bla();
}

Then all You have to do to access it is this:

Bla.current.variable = x;

Of course not, but if you want some data in some class then it has to exist somewhere, right? Well, if it isn’t going to be a Monobehavior (something attached to a GameObject) then it has to be a variable inside something that already exists (a Monobehavior in the scene) or have a static instance of that class so it always exists in the background.

That’s pretty fundamental stuff, you may want to go do the Unity Tutorials in the Learn section if that kind of thing is giving you a hard time.

would it be wise through to have a UI be static? especially Player UI like health,mana etc?

No, build a UI manager that references those UI elements and communicate to that.

Before we all forget about this
NO, GameObject.Find is something that should be eradicated completely, use gamepbject.FindGameObjectWithTag(tagHere)

1 Like

and i assume this UI manager got to be static?

also the UI Script is attached to a Canvas so its a gameobject if i am correct?

This one is not attached to any gameobjects

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
public class Player : BasePlayer {




    public Player()
    {
     



        GameObject go = (GameObject)MonoBehaviour.Instantiate(Resources.Load("Player"),new Vector3(502,20,-608),Quaternion.identity)as GameObject;
        MonoBehaviour.DontDestroyOnLoad (go.transform);


        //GameObject go = MonoBehaviour.Instantiate(Resources.Load("MyPrefab")) as GameObject;
        LoadInformation.LoadAllInformation ();

        PlayerName = GameInformation.playerName;
        PlayerLevel = GameInformation.playerLevel;
        PlayerHealth = GameInformation.playerHealth;
        PlayerMaxHealth =    GameInformation.playerMaxHealth;
        PlayerCurrentHealth =    GameInformation.playerCurrentHealth;
        PlayerMana =    GameInformation.playerMana;
        PlayerStrength =    GameInformation.playerStrength;
        PlayerIntellect =    GameInformation.playerIntellect;
        PlayerAgility =    GameInformation.playerAgility;
        PlayerLuck =     GameInformation.playerLuck;
        PlayerAttackSpeed =    GameInformation.playerAttackSpeed;
        PlayerCastSpeed =    GameInformation.playerCastSpeed;
        PlayerSpeed =     GameInformation.playerSpeed;


PlayerUI._Instance.healthbar.value = PlayerHealth;

        Debug.Log ("PlayerHealth " + PlayerHealth);

   }




}
This one is attached to a Canvas and its the one i have problems with
also its in another scene which i load once a player is created.

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

public class PlayerUI : MonoBehaviour {


    public static PlayerUI _Instance { get; private set;}

    public Slider healthbar;
    public Slider manabar;
    public Text healthtext;
    public Text manatext;




    // Use this for initialization
    void Awake () {

        if (_Instance == null) {
            DontDestroyOnLoad(this);
            _Instance = this;
        }
        else {
          
            DestroyImmediate(this);
        }


    }

    void Start(){
        PlayerUI._Instance.healthbar = GameObject.Find ("HealthBar").GetComponent<Slider> ();
        PlayerUI._Instance.manabar = GameObject.Find ("ManaBar").GetComponent<Slider> ();
        PlayerUI._Instance.healthtext = GameObject.Find ("Hp_Amount").GetComponent<Text> ();
        PlayerUI._Instance.manatext = GameObject.Find ("Mp_Amount").GetComponent<Text> ();
    }


  
    // Update is called once per frame
    void Update () {
      
    }
}

i feel like it’s losing it somewhere and doesn’t keep the data ?

okay so after doing a couple of changes. i made the UI instantiate as a prefab which seems to work,

What if the script you want is in an object that is also a child of more than one object?