Search Unity

Accessing Prefab's field before instantiation

Discussion in 'Editor & General Support' started by Sumihomura, Jul 20, 2017.

  1. Sumihomura

    Sumihomura

    Joined:
    May 19, 2017
    Posts:
    33
    I'm creating a game strongly based on an OOP approach, and I've encountered this problem:

    At a given point, I have to buy an animal among many, each one with it's own class inheriting from a superclass.
    To do so, I have to check the price of the animal before instantiation, so I should access the script attached to the prefab(but considering that each script has a different name, so I can't just do GetComponent<ScriptName>() )

    I've found a couple of solutions by having a list of the prices in the shop class or instantiating the gameObject, checking if the price is ok and, if not, destroying it, but both the solutions are not very OOP friendly, i suppose.

    Any suggestion?

    Thx! :)
     
  2. Sumihomura

    Sumihomura

    Joined:
    May 19, 2017
    Posts:
    33
  3. Sumihomura

    Sumihomura

    Joined:
    May 19, 2017
    Posts:
    33
    Bump, again. Sorry but I'm still looking for a solution.
     
  4. BFS-Kyle

    BFS-Kyle

    Joined:
    Jun 12, 2013
    Posts:
    883
    How do you check the price of the object after instantiating it? Can you not just do that same check BEFORE instantiating it?

    e.g.
    Code (CSharp):
    1. GameObject _Prefab = Resources.Load("MyPrefab");
    2.  
    3. PriceComponent _Price = _Prefab.GetComponent<Price>();
    4.  
    5. if(_Price.Cost < 100)
    6. {
    7.     //Instantiate it now
    8. }
    You mentioned you "can't just do GetComponent" - so just replace the GetComponent bit with whatever you actually do to check the price.
     
    Sumihomura likes this.
  5. Sumihomura

    Sumihomura

    Joined:
    May 19, 2017
    Posts:
    33
    Thanks for your reply. I've managed to find the solution I was looking for.
    Basically you can call the superclass as component of the animal and call any method overriden by the subclass your prefab is binded to. The method will work as if called by the subclasses the prefab is binded to.

    anyAnimalPrefab.GetComponent<AnimalScript>().Cost

    will return the cost of the specific animal your pointing at, no matter if is a MouseScript animal or ZebraScript animal.

    Hope it will help somebody else. :)
     
    old_pilgrim likes this.
  6. old_pilgrim

    old_pilgrim

    Joined:
    Nov 15, 2019
    Posts:
    7
    Good solution, thanks.

    For those who do not understand what the OP is asking: say we have a base script class,

    Code (CSharp):
    1. public abstract class BaseAnimal : MonoBehaviour {
    2.     public int price;
    3. }
    and then specific implementations of animals that inherit from this,

    Code (CSharp):
    1. public class MyAnimal : BaseAnimal {
    2.     // whatever
    3. }
    Now we need to check the price of any animal before instantiation. To do that, we can simply get the base class component on the prefab, i.e.

    Code (CSharp):
    1. int myPrice = myAnimalPrefab.GetComponent<BaseAnimal>().price;