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 Pempti · May 16, 2014 at 09:21 AM · javascriptmaterialobjectassign

Cannot Assign Material To Object

Hi guys, I'm having a problem with assigning a material to an Object which is an instance of a class. In the Inspector, I've dragged the Material I want but when I print out the value of the material, it gives me null.

 public class Monster extends MonoBehaviour {
    var material : Material;
 
    //Constructor
    public function Monster() {
    print(material); //This gives null even though the material     is clearly assigned in the Inspector
    }
 }
 
 //I call this constructor using:
 var monster : Monster = new Monster();

I'm completely stumped on this so any help is much appreciated.

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 Joyrider · May 16, 2014 at 09:22 AM 0
Share

how are you assigning a material in the editor for a class that you are instantiating at runtime?

avatar image Pempti · May 16, 2014 at 10:54 AM 0
Share

I've created a prefab for the class just so I can assign a material in the inspector. I have a feeling that the problem arises due to the fact that even though I'm calling the constructor, I don't actually make an instance of the $$anonymous$$onster. Somehow I CAN still retrieve all the other fields such as health or defence of the $$anonymous$$onster class but the material field just doesn't want to be initialised, thus always returning a null value.

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by sriram90 · May 16, 2014 at 10:02 AM

you shouldn't implement (and definitely shouldn't call) constructors for MonoBehaviours. One of the reasons for this is that in the best-case scenario, your constructor will be called when the game starts, and then the contents of the instance will be over-written by de-serialized data as Unity loads up your scene file.

If you want initialization code to run when the game starts, use Start() or Awake(). Awake() will be called immediately as part of the instantiation process. Start() will be called before the first Update() call that the newly instantiated object gets.

ScriptableObjects are non-Behaviour Objects that Unity lets you serialize as stand-alone assets. They're for storing data that doesn't belong to a specific level or prefab. GUISkins are a good example of a ScriptableObject.

Source : Check here for better explanation

Comment
Add comment · Show 3 · 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 Pempti · May 16, 2014 at 10:58 AM 0
Share

Honestly, the only reason I've even got the class extending $$anonymous$$onoBehaviour is because I can't otherwise access the Inspector to drag and drop the material that I want to use for that $$anonymous$$onster. I don't actually want to Instantiate the $$anonymous$$onster prefab, rather I just want it to store a bunch of fields for the stats of the monster and a material (which includes a texture) so I can recognise the monster.

avatar image sriram90 · May 19, 2014 at 06:06 AM 0
Share

You just need to avoid constructor in monobehaviour. Just keep your code (Whatever you want to do) in start() function.

avatar image flamy · May 20, 2014 at 05:48 AM 0
Share

declare the variable as public, you will see in inspector! and dont use Constructor and use Awake or start which is the equivalent for Constructors for monobehaviour!

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

Assign Material by script: Javascript? 1 Answer

A surface that has multiple physical materials? 0 Answers

Move object to raycast point. 3 Answers

Can someone help me fix my Javascript for Flickering Light? 6 Answers

Random instantiation at an object 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