Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Using classes on a object instantiate

Discussion in 'Scripting' started by luckie12, Jul 13, 2016.

  1. luckie12

    luckie12

    Joined:
    Aug 17, 2014
    Posts:
    165
    Hello, i have a question, i made this class:

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class Buildings : TileMap {
    5.     private int _Cost;
    6.     private int _maxBuilding;
    7.     private BuildingType _buildType;
    8.  
    9.     public void BType()
    10.     {
    11.         _Cost = 1000;
    12.         _maxBuilding = 1;
    13.         _buildType = BuildingType.Attack;
    14.         return;
    15.     }
    16.  
    17.     public void BType(int cost, int mBuilding, BuildingType bt)
    18.     {
    19.         _Cost = cost;
    20.         _maxBuilding = mBuilding;
    21.         _buildType = bt;
    22.     }
    23.  
    24.     public int Cost
    25.     {
    26.         get { return _Cost; }
    27.         set { _Cost = value; }
    28.     }
    29.  
    30.     public int mBuilding
    31.     {
    32.         get { return _maxBuilding; }
    33.         set { _maxBuilding = value; }
    34.     }
    35.  
    36.     public BuildingType TypeOfBuilding
    37.     {
    38.         get { return _buildType; }
    39.         set { _buildType = value; }
    40.     }
    41.  
    42. }
    43. public enum BuildingType
    44. {
    45.     Attack,
    46.     Economy
    47. };

    But i dont know that well how to access/use it, on my main script i use a Instantiate object on click,

    Code (CSharp):
    1.     public void CreateBuilding(int x, int y)
    2.     {
    3.         GameObject B1 = (GameObject)Instantiate(Building, new Vector3(x, y, -1f), Quaternion.identity);
    4.     }
    But, i want to pass these values to the instantiate object with

    Cost; 1500
    Maxbuilding; 3
    and
    BuildingType; Economy

    How can i achieve this?

    Do i need to create a script for each Building and access that class?

    Thanks for the help !
     
  2. SkaredCreations

    SkaredCreations

    Joined:
    Sep 29, 2010
    Posts:
    296
    Assign B1.GetComponent<Buildings>() to a variable after Instantiate and call the BType method passing the new settings.
     
  3. luckie12

    luckie12

    Joined:
    Aug 17, 2014
    Posts:
    165
    I have this now

    Code (CSharp):
    1.         GameObject B1 = (GameObject)Instantiate(Building, new Vector3(x, y, -1f), Quaternion.identity);
    2.         B1.GetComponent<Buildings>();
    No clue on what u mean with call the BTypee :p or when to use it

    EDIT changed it to:

    Code (CSharp):
    1.         GameObject B1 = (GameObject)Instantiate(Building, new Vector3(x, y, -1f), Quaternion.identity);
    2.         B1.GetComponent<Buildings>();
    3.         BTypee.Cost = 200;
    4.         BTypee.mBuilding = 2;
    5.         BTypee.TypeOfBuilding = BuildingType.Attack;
    Should this work like this?
     
  4. SkaredCreations

    SkaredCreations

    Joined:
    Sep 29, 2010
    Posts:
    296
    "Attack" is not a variable, accordingly to your class declaration TypeOfBuilding should be a valid enum value and also you're not assigning anything....

    Code (CSharp):
    1. GameObject B1 = (GameObject)Instantiate(Building, new Vector3(x, y, -1f), Quaternion.identity);
    2. Buildings BTypee = B1.GetComponent<Buildings>();
    3. BTypee.Cost = 200;
    4. BTypee.mBuilding = 2;
    5. BTypee.TypeOfBuilding = BuildingType.Attack;
     
  5. luckie12

    luckie12

    Joined:
    Aug 17, 2014
    Posts:
    165
    I get

    Code (CSharp):
    1. NullReferenceException: Object reference not set to an instance of an object
    when i instantiate it, on this line:

    Code (CSharp):
    1.         BTypee.Cost = 200;
    Added this:
    public Buildings BTypee;
    and now no errors when i instantiate, is there a way to check my building specification ?
     
  6. SkaredCreations

    SkaredCreations

    Joined:
    Sep 29, 2010
    Posts:
    296
    Read my answer, you're doing it wrong.
     
  7. luckie12

    luckie12

    Joined:
    Aug 17, 2014
    Posts:
    165
    Aaah i see! and how would this work when i have more buildings with different specifications, would i make a if statement for that? or something like that
     
  8. calebc01

    calebc01

    Joined:
    Jul 21, 2015
    Posts:
    62
    I'm presuming that the parent TileMap is derived from a Monobehavior class?

    When you instantiate a GameObject, all attached components are created as well. So if you have a building script attached, it creates a building object and attaches it as a child to the game object.

    The argument that you pass to Instantiate ('Building' in your script) should be a reference to a prefab that you want to clone. i.e.

    Code (csharp):
    1.  
    2. // Drag and drop your building object prefab to this item in the editor
    3. public GameObject BuildingPrefab;
    4.  
    5. // ...
    6.  
    7. public void CreateBuilding(int x, int y)
    8. {
    9.      GameObject B1 = (GameObject)Instantiate(BuildingPrefab, new Vector3((float)x, (float)y, -1f), Quaternion.identity);
    10.      Building theBuilding = B1.transform.GetComponent<Buildings>();
    11.  
    12.      if (theBuilding != null)
    13.      {
    14.          // Do stuff...
    15.      }
    16. }
    17.  
    18.  
     
  9. luckie12

    luckie12

    Joined:
    Aug 17, 2014
    Posts:
    165
    This is my full code as it is NOW, but im still stuck on achieving that whenever i instantiate another object, it gets other values like cost = 500 etc

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections.Generic;
    3. using System.Linq;
    4.  
    5. public class TileMap : MonoBehaviour {
    6.  
    7.     public GameObject Building;
    8.     public TileType[] tileTypes;
    9.  
    10.     public Buildings BTypee;
    11.  
    12.     int mapSizeX = 10;
    13.     int mapSizeY = 10;
    14.  
    15.     int[,] tiles;
    16.  
    17.     // Use this for initialization
    18.     void Start () {
    19.         GenerateMapData();
    20.         GenerateMapVisuals();
    21.     }
    22.  
    23.     void GenerateMapData()
    24.     {
    25.         tiles = new int[mapSizeX, mapSizeY];
    26.  
    27.         int x, y;
    28.         //grass
    29.         for (x = 0; x < mapSizeX; x++){
    30.             for (y = 0; y < mapSizeY; y++){
    31.                 tiles[x, y] = 0;
    32.             }
    33.         }
    34.     }
    35.  
    36.  
    37.  
    38.     void GenerateMapVisuals()
    39.     {
    40.         for (int x = 0; x < mapSizeX; x++)
    41.         {
    42.             for (int y = 0; y < mapSizeY; y++)
    43.             {
    44.                 TileType tt = tileTypes[tiles[x, y]];
    45.                 GameObject GO = (GameObject)Instantiate(tt.tileVisualPrefab, new Vector3(x, y, 0), Quaternion.identity);
    46.                 ClickableTile ct = GO.GetComponent<ClickableTile>();
    47.                 ct.tileX = x;
    48.                 ct.tileY = y;
    49.                 ct.map = this;
    50.             }
    51.         }
    52.     }
    53.  
    54.     public void CreateBuilding(int x, int y)
    55.     {
    56.  
    57.         GameObject B1 = (GameObject)Instantiate(Building, new Vector3(x, y, -1f), Quaternion.identity);
    58.         Buildings BTypee = B1.GetComponent<Buildings>();
    59.         BTypee.Cost = 200;
    60.         BTypee.mBuilding = 2;
    61.         BTypee.TypeOfBuilding = BuildingType.Attack;
    62.  
    63.         Debug.Log(BTypee.Cost + " " + BTypee.mBuilding);
    64.     }
    65.  
    66.  
    67.  
    68.     /*
    69.     public void RemoveSelectedUnit(int x, int y)
    70.     {
    71.         Destroy(gameObject);
    72.     }
    73.     */
    74. }
    75.  
     
    Last edited: Jul 13, 2016