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 /
This post has been wikified, any user with enough reputation can edit it.
avatar image
0
Question by Lawlhwut · Feb 05, 2015 at 11:25 AM · c#object

Writing property of object to the console?

 using UnityEngine;
 using System.Collections;
 
 public class Products : MonoBehaviour {
     
     public string Name { get; set;}
     public string Manufacturer { get; set; }
     public float CosttoPurchase { get; set; } //When buying from manufacturer
     public float AmountinPackage { get; set; }
     public float CostofProduct { get; set; } //When selling at shop
     public float Size { get; set; }
     public float Popularity { get; set; }
     public float Tier { get; set; }
     public Products (string name, string manufacturer, 
                      float costtopurchase, float amountinpackage, 
                      float costofproduct, float size, 
                      float popularity, float tier) {        
         Name = name;
         Manufacturer = manufacturer;
         CosttoPurchase = costtopurchase;
         AmountinPackage = amountinpackage;
         CostofProduct = costofproduct;
         Size = size;
         Popularity = popularity;
         Tier = tier;
     }
     Products toiletpaper1 = new Products ("Scotty", "Scotty", 2, 12, 0, 5, 80, 1);
     
     
     // Use this for initialization
     void Awake () {
         //Products toiletpaper1 = new Products ("Scotty", "Scotty", 2, 12, userSelect, 5, 80, 1);
     }
     
     // Update is called once per frame
     void Update () {
         Debug.Log (toiletpaper1.???); //What do I put here??????
     }
 }

Im trying to get a property (name, manufacturer, costofproduct, etc) from the instance of Products, toiletpaper1, but when I try Debug.Log(tioletpaper1.Name) I get Nullreference error message. What am I supposed to put there?

On a side note, where would I go to learn a bit more about this topic? I am not too good with the programming vocabulary...

Comment
Add comment · Show 2
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 meat5000 ♦ · Feb 05, 2015 at 03:13 AM 0
Share

Separate them.

  using UnityEngine;
  using System.Collections;
  
  public class Products : $$anonymous$$onoBehaviour {
      
     public string Name { get; set;}
     public string $$anonymous$$anufacturer { get; set; }
     public float CosttoPurchase { get; set; } //When buying from manufacturer
     public float AmountinPackage { get; set; }
     public float CostofProduct { get; set; } //When selling at shop
     public float Size { get; set; }
     public float Popularity { get; set; }
     public float Tier { get; set; }
     public Products (string name, string manufacturer, 
                      float costtopurchase, float amountinpackage, 
                      float costofproduct, float size, 
                      float popularity, float tier) {        
         Name = name;
         $$anonymous$$anufacturer = manufacturer;
         CosttoPurchase = costtopurchase;
         AmountinPackage = amountinpackage;
         CostofProduct = costofproduct;
         Size = size;
         Popularity = popularity;
         Tier = tier;
     } 
 }

One for the Type you created and one to use it.

 using UnityEngine;
 using System.Collections;
  
 public class ToiletCall : $$anonymous$$onoBehaviour {
 
     Products toiletpaper1 = new Products ("Scotty", "Scotty", 2, 12, 0, 5, 80, 1);
 
     void Update () {
          Debug.Log (toiletpaper1.Name); //What do I put here??????
     }
 }


avatar image Bonfire-Boy · Feb 05, 2015 at 12:11 PM 0
Share

That's not how you create monobehaviour instances in code. You want to create a Gameobject and add a Products component. Something like...

 GameObject bogRollObject = new GameObject();
 bogRollObject.AddComponent< Products >();
 bogRollObject.GetComponent< Products>.init(...); // This does the work of your ctor, you don't want a ctor on a $$anonymous$$onobehaviour

1 Reply

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

Answer by Baste · Feb 05, 2015 at 12:52 PM

Turn on your console warnings!

When you did this before you commented it out:

 void Awake () {
     Products toiletpaper1 = new Products ("Scotty", "Scotty", 2, 12, userSelect, 5, 80, 1);
 }

You would have gotten this warning from unity:

"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"

Which is exactly the information you need. The Product class you're trying to write doesn't look like a behavior - it's not something that needs to have a Transform or have Update called on it regularly - so you can safely declare it as a normal class instead of a MonoBehaviour.

Oh, and the class represents a single product, so it should be named "Product" instead of "Products". And instead of having comments that say "When buying from manufacturer" and "When selling at shop", just rename your variables:

 CosttoPurchase -> ManufacturerPrice
 CostofProduct -> ShopPrice
Comment
Add comment · 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

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Trouble grabbing and holding a cube. 0 Answers

Initialising List array for use in a custom Editor 1 Answer

Manipulating Instanced Object 2 Answers


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