Instantiate scriptableobject with random variables

1.3k Views Asked by At

I just started to work with scriptableobject, and I'm trying to Instantiate / Create Instance of a very basic scriptableobject that got 4 line of arrays.

I'm trying to Instantiate one scriptableobject that I created, that every time he's been created he will randomize his variables

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

[CreateAssetMenu(fileName = "New Character",menuName = "Character Creation/Player Units")]
public class CharStats : ScriptableObject
{
    [SerializeField]
    public string [] charName;
    public string [] charDescription;
    public string [] charSin;
    public Sprite [] charIcon;
}

The changing I'm doing do inside the inspector for one scriptableobject, and just his inside variables will change every time I create new one. Is it possible to achieve?

1

There are 1 best solutions below

0
On

Ok so I manage to create something might be not the right way, but after pressing a key ( for now testing) its Instantiate my scriptableobjectwith a random Icon I put on for now. Ill add the 3 scripts, bad names coz it's for testing only, (testing - its script that sitting on empty gameobject with reference to my scriptableobject, testing 2 - its script that sitting on a prefab that I Instantiate with the scriptableobject)

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Random = UnityEngine.Random;

public class testing2 : MonoBehaviour
{
    //public ScriptableObject person;
    public CharStats charSats;

    public Sprite sprite2;
    private SpriteRenderer spriteRender;
    private void Start()
    {
        spriteRender = GetComponent<SpriteRenderer>();
        GetRandomSprite();

    }

    public Sprite GetRandomSprite()
    {
        int randomIndex = Random.Range(0, charSats.charIcon.Length);

        
        Debug.Log(charSats.charIcon[randomIndex]);

        if (sprite2 == null)
        {
            sprite2 = charSats.charIcon[randomIndex];
            spriteRender.sprite = sprite2;

        }
        return charSats.charIcon[randomIndex];
        
    }
}

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Random = UnityEngine.Random;

public class Testing : MonoBehaviour
{
    public GameObject Player;

    private void Update()
    {
        SpawnPlayer();
    }

    public void SpawnPlayer()
    {
        if (Input.GetKeyDown(KeyCode.A))
        {
            GameObject spawnPalyer = Instantiate(Player, new Vector3(Random.Range(-10,10), Random.Range(-10, 10)), Quaternion.identity) as GameObject;
        }

    }


}

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

[CreateAssetMenu(fileName = "New Character",menuName = "Character Creation/Player Units")]
public class CharStats : ScriptableObject
{
    [SerializeField]
    public string [] charName;
    public string [] charDescription;
    public string [] charSin;
    public Sprite [] charIcon;

    
}