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.
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.
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.
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.
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 ?