Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by arrowfan2 · Apr 28, 2014 at 11:19 PM · newgetset

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);
         
     }
 }

}

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image ByteSheep · Apr 28, 2014 at 11:49 PM 1
Share

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)

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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.

Comment
Add comment · Show 8 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image shopguy · Apr 29, 2014 at 12:03 AM 0
Share

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.

avatar image getyour411 · Apr 29, 2014 at 02:27 AM 0
Share

From his karma, you can see that @robertbu answers thousands of questions. Sometimes we start the conversation with comments and see where it unfolds.

avatar image arrowfan2 · Apr 29, 2014 at 01:21 PM 0
Share

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?

avatar image robertbu · Apr 29, 2014 at 03:47 PM 0
Share

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.

avatar image arrowfan2 · May 11, 2014 at 02:29 PM 0
Share

okay i used the AddComponent way and it works as intended except that i still cant write information to pokemondata.

Show more comments

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

23 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges