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.

Unity 3D and JSON to control game objects and different properties

Discussion in 'Scripting' started by deepsync26, Jan 4, 2019.

  1. deepsync26


    Oct 30, 2018
    First of all let me start off by saying what you're about to read might totally irritate you because this might be the stupidest question asked but I seem to be running into dead ends here. I have a hazy picture of the solution I just need someone maybe to outline it so I can work on this.

    I have a Unity 3d game where I have to instantiate 10 enemies(cubes, in my case) at runtime at random positions which are destroyed by player on collision. I have 2 different kinds of cubes to be instantiated. I can easily do that with c# script since I only have 2 cubes. But I want to integrate Json and store 2 parameters in that file : 1. Colour of the cube ( 2 different colours for 2 items) and 2. an Int value to put as points gained when that kind of cube is destroyed. I am able to create a Json file to store these parameters but I have no idea on how to use those parameters in particularly to instantiating GameObjects. Any help would be a huge favour.
    may be just short steps on what steps would be followed in the script to make this possible? Just simple steps would work, I would refer further material on the internet to solve those. Its just that Json for unity has so much confusing stuff out there I'm not sure what to do. serialisers, parsers, etc. I know a bit about these terms but i get confused on what all do i need to even read to solve this problem. Thanks again though!
  2. Kurt-Dekker


    Mar 16, 2013
    Do you need to use JSON? What you're describing seems custom-made for Unity's built-in ScriptableObject class, which are first-class citizens within the Unity editor inspector.

    For instance, here might be a class to do exactly what you say:

    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    4. [CreateAssetMenu]
    5. public class CubeDescriptor : ScriptableObject
    6. {
    7.     public Color MyColor;
    8.     public int MyPoints;
    10.     void Reset()
    11.     {
    12.         MyColor =;
    13.         MyPoints = 10;
    14.     }
    15. }
    In your assets folders somewhere you can right click and make as many variations of that as you like, each one with different properties.

    You could even add a "public GameObject WhatILookLike;" field and point each one to a different prefab!

    If you insist on JSON, you can do that too. Just make a [System.Serializable] class and follow the data shape. There are websites to help you generate JSON data and/or C# classes that match each other precisely.