Quantcast
Channel: Questions in topic: "get"
Viewing all articles
Browse latest Browse all 215

Instead of "new" for accessing properties?

$
0
0
hey guys! so im pretty new to unity but i know a little bit about programming. so as the title says. im looking for an alternative way of access my properties. also im trying to figure out how i can set values to the properties. im working on a pokemon fangame. the thing is that i want to have a individual PokemonData to be attached to each pokemon. which will be spawned in by TheSpawnscript but debug.log shows that it doesnt work. PokemonData pcs = new PokemonData(); gives the warning"you are trying to create a MonoBehaviour using the 'new' keyword. This is not allowed. MonoBehaviours can only be added using AddComponent(). Alternatively, your script can inherit from ScriptableObject or no base class at all UnityEngine.MonoBehaviour:.ctor()" sorry for sucking at describing! ---------- public class TheSpawnScript: Monobehaviour{ void Start () { PokemonStatsGenerator(); } void PokemonStatsGenerator(){ PokemonData pcs = new PokemonData(); pcs.Id = 1238974; pcs.Level = 4.0f; pcs.Sex = Random.Range(0f,1f); pcs.Number = 152; } } ---------- public class PokemonData : MonoBehaviour { float level; float sex; int number; int id; public float Level{ get{ return level; } set{ level = value; } } public float Sex{ get{ return sex; } set{ if(sex < 0.5f){ Mathf.RoundToInt(0); }else{ Mathf.RoundToInt(1); } } } public int Number{ get{ return number; } set{ number = value; } } public int Id{ get{ return id; } set{ id = value; } } } ---------- public class getinfo : MonoBehaviour { // Update is called once per frame void Update () { if(Input.GetButtonDown("Jump")){ PokemonData pcs = new PokemonData(); Debug.Log("Level " +pcs.Level); Debug.Log("Sex " +pcs.Sex); Debug.Log("Number " +pcs.Number); Debug.Log("Id " + pcs.Id); } } }

Viewing all articles
Browse latest Browse all 215

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>