• 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
2
Question by pajamajama · Dec 13, 2013 at 03:50 AM · playmovietexturecastcasting

Why do I have to explicitly cast my MovieTexture?

Shouldn't unity automatically know that the maintexture is a movietexture and allow access to Play()? It just seems a little clunky, but I'm by no means an expert programmer.

Currently I have to use the following code and cast the texture as a MovieTexture before I can play it:

 MovieTexture movieTexture;
 
     // Use this for initialization
     void Start () {
         movieTexture = renderer.material.mainTexture as MovieTexture;
         movieTexture.Play ();
     }

Thank you for any explanations!

Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
2

Answer by Bunny83 · Dec 13, 2013 at 04:03 AM

No ;)

Strong typed languages always require casting in such situations. That's because a material can reference any type of Texture. It could be a Texture2D or a MovieTexture. The material just needs a "Texture". Since Texture2D and MovieTexture are derived classes from Texture they can be assigned to mainTexture, however the Material just knows it has a material. Casting actually performs a type check at runtime.

The normal cast throws an exception if the cast fails (for example when you have a Texture2D but you try to cast it to a MovieTexture).

     movieTexture = (MovieTexture)renderer.material.mainTexture;

The as-cast doesn't throw an exception but if the cast fails it simply returns null. In some situation that's quite useful. However in most cases this is hard to debug since the runtime doesn't tell you what's actually wrong. In your code, if the cast fails, you would get a null-reference-exception in line 6 but the actual (logical) mistake was in line 5.

That's why you usually use the normal cast.

Comment
Add comment · Show 1 · 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 pajamajama · Dec 13, 2013 at 04:36 AM 0
Share

Wonderful feedback as always Bunny83! Thank you!

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

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

17 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

Related Questions

C# - Resources.Load Sprite trouble 2 Answers

Changing texture when movie is paused 0 Answers

play movie texture smoothly in unity 0 Answers

How to cast/convert a ControllerColliderHit into a Prefab 2 Answers

Is There A Way To Cast On 1 Returning Value 2 Times? 5 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