Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 Saeesh · Mar 11, 2018 at 07:07 PM · unity5projectorframe

Set texture of projector

I want to change the frame of projector but I get this error :

"NullReferenceException: Object reference not set to an instance of an object " at this line

projector.material.SetTexture("_ShadowTex", frames[frameIndex]);

This is the code I've used

 public float fps = 30.0f;
 public Texture2D[] frames;

 private int frameIndex;
 private Projector projector;

 void Start()
 {
     projector = GetComponent<Projector>();

     NextFrame();
     InvokeRepeating("NextFrame", 1 / fps, 1 / fps);
 }

 void NextFrame()
 {
     projector.material.SetTexture("_ShadowTex", frames[frameIndex]);
     frameIndex = (frameIndex + 1) % frames.Length;
 }
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
0

Answer by Vicarian · Mar 14, 2018 at 05:54 PM

This answer can go a couple different ways, given lack of knowledge of the problem. If your frames collection only has one texture in it, the call to NextFrame as InvokeRepeating starts will aways target an incorrect (non-existent) array index as x modulo 0 is always x. So, the call will attempt to fetch the 2nd element of a 0 index array having length 1, which fails.

Alternatively, it's possible that trying to access the material field of the projector without first initializing it is what causes the NullRef. You'll want to make Materials in the editor for each possible shadow texture then modify the script to use a collection of Materials rather than Texture2Ds. Then you'll add the materials you made to the Materials collection of this script. Again, the failure state will occur if the materials collection only has one element.

I'd change the line using the modulus to a conditional to avoid the 1-element failure state:

 frameIndex++;
 
 if (frameIndex > frames.Length)
     frameIndex = 0;


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

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

Projectors issue/Bug after upgrading to Unity 5 3 Answers

Ambient Light resets during Play mode 0 Answers

After upgrading to Unity5, getting NSURLErrorDomain error -1012 in iOS 1 Answer

Tracking user stages in Unity Analytics -1 Answers

vehicle control unity 5 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