- Home /
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...
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??????
}
}
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
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
Your answer
Follow this Question
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