- Home /
Too subjective and argumentative
Getting value of disabled object into initiated prefab
It's fine for me to get its value to existed in scene objects with getComponent in Start(). But when I need these values in initiated objects I can't access them simply because the object, where these values in, disabled by the time I instantiate another object, where I need these values. I hope it's not too confusing, mb some code will help to understand. object where I need this value: void Start() { house = GameObject.Find("HousingPanel").GetComponent(); // this line is wrong for an initiated object }
void Update()
{
if (score.i <= house.capacity) // score object is fine, cos it isn't disabled
// some code
}
object where they store:
public class housingPanelScript : MonoBehaviour
{
public int capacity = 10;
//some code
public void upgradeHouse()
{
capacity += 10;
}
}
Answer by Link17x · Mar 18, 2020 at 01:44 AM
First, it's bad practice to use "Find", you should set up a reference that's more reliable/safe.
You can either not disable it, instead move it off the screen or if it's a UI element use Canvas Group to make it inactive/invisible. Then you could still access the value Your other option is to have some script somewhere that you can save the data to, then you can access it from that place instead (provided that it's a value type not reference).
Alternatively, if it's just the score maybe you could consider using PlayerPrefs.SaveInt or SaveFloat. Give it a name and the value to save, then load it out using the same name you gave it when you saved it. Bear in mind, this will save it in memory. So when you restart your game, that value will be saved. This might be useful if you want the score to be persistent and use it have a high score or something... Otherwise you could do PlayerPrefs.DeleteAll on start, but to be honest it's probably not what you want if you intend to reset the score every play. It's just an option to consider.
The score is working fine because a reference of the object is set-up and the object isn't disabled but capacity is in an object that's disabled. I'm already using canvas to disable that object and I can't access it. Good tip about moving it out of the screen, but isn't it memory eating if there will be a lot of these UI panels? Btw why find() is unsafe? Cos may not find this object by name?
I'm a little confused, capacity is an object? Does it really need to be? And how is it disabled? It might help to see the code for this.
Canvas is different to the canvas group component in case you were confusing the two.
It depends, how many are you instantiating and are you reusing them? Sometimes it's more efficient to hide/show than constantly delete and instantiate. If they're not used that often then it's probably more ideal to just delete and recreate as and when you need.
It is inefficient because it loops through every gameobject in your scene. Plus, if you ever change the name of the gameobject or delete it, then this code won't produce any error at compile time often making it harder to spot the issue. If you're instantiating lots of the same object, I'm not sure if it might have issues finding the right one too, since they're copies with the same name? Overall, it's best to avoid.
Oh wait, I've read through again. So your capacity isn't really an object. It shouldn't really inherit from $$anonymous$$onobehaviour if you want to use it as an object. Then you can construct a new score in your other script and access a public method to increase the score i.e.
public class ScoreObject
{
public float Score { get; private set; }
public void IncreaseScore(float amount)
{
Score += amount;
}
}
public class Some Class : $$anonymous$$onobehaviour
{
private ScoreObject m_ScoreObject;
private void Start()
{
m_ScoreObject = new ScoreObject();
}
private void Update()
{
m_ScoreObject.IncreaseScore(1);
}
}
So that's set up an object to handle score. You can access it through that object now. Obvious you probably won't want Update adding score like that but it's an example to show you how you can use it. Just bear in $$anonymous$$d if you have this script on each prefab you instantiate, a new score will be created for each. If you want one overall score then perhaps do this but store it in some Game $$anonymous$$anager class that they can access (or create a static Score $$anonymous$$anager class that can be accessed anywhere)
$$anonymous$$y score is okay. I know that there's no need to assign it to a prefab. So there's a deal.
Player initiate a bunch of similar characters. And I need that capacity value from another class to check if a player still can initiate these characters or not. But I can't get that value because characters haven't initiated yet and class with capacity value is already disabled. So how I can get this value? Simply not disable that class, but ins$$anonymous$$d 'hide' it from a camera? And how bad it will be with performance if I keep hiding it and not disable/destroying them?