• 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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



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

how can i assign a material to a 3d model? 1 Answer

JSON to object 0 Answers

Object Rotation Limit! (JavaScript) 1 Answer

Changing materials of a plane on keydown. 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges