- Home /
Instead of "new" for accessing properties?
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);
}
}
}
Here is also a short unity tutorial with some example code on properties https://unity3d.com/learn/tutorials/modules/intermediate/scripting/properties (as you can see the Player class doesn't derive from $$anonymous$$onoBehavior)
Answer by robertbu · Apr 28, 2014 at 11:46 PM
I see no reason why 'pokemonData' should be derived from MonoBehaviour. Just remove the ': MonoBehaviour' from the class declaration and the use of 'new' should be fine.
I think you are right robertbu, should post as answer so that others (like me) trying to answer unanswered questions to help others, will not have to read it all to find out there is already and answer.
From his karma, you can see that @robertbu answers thousands of questions. Sometimes we start the conversation with comments and see where it unfolds.
so i deleted the monobehaviour in pokemonData and the warning vanished. but i still cant write data from thespawnscript to the property in pokemondata. also dont i need the monobehaviour if i want to have the script attached to a gameobject?
I don't know how you've structured things. If you want to attach it to a game object as a component, then you do have to derive it from $$anonymous$$onoBehaviour. Then you would create it by AddComponent(), not by the 'new' operator. Something derived from $$anonymous$$onoBehaviour only exists as a component of a GameObject. Alternately you can not derive it from $$anonymous$$onoBehaviour, and simply reference it from a script attached to the game object. That is a component on the game object will have a variable referencing an instance of your PokemonData class. I don't see enough of the structure of your game to advise you on which way to go.
okay i used the AddComponent way and it works as intended except that i still cant write information to pokemondata.
Your answer
Follow this Question
Related Questions
PlayerPref/PlayerPrefsX why not able to store boolean values? 1 Answer
Public variable hidden in the inspector 2 Answers
problem with get{} and set{} 1 Answer
c# setter/getters 1 Answer
Overriding a function. 1 Answer