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 OtsegoDoom · Jun 10, 2011 at 12:03 AM · textureinstantiatetimeanimated

Animated Texture and Instantiation

Hi Everyone,

Over the last couple of months I've been trying to wrap my head around some programming in Unity but I am NOT a programmer, so I've been running into a lot of roadblocks.

I've been working with a script to animate textures that I grabbed from the Unity Wiki:


 var uvAnimationTileX = 4; 
 //Here you can place the number of columns of your sheet.
 
 var uvAnimationTileY = 1; 
 //Here you can place the number of rows of your sheet.
 
 var framesPerSecond = 1.0;
 
 function Update () {
 
     // Calculate index
     var index : int = Time.time * framesPerSecond;
     // repeat when exhausting all frames
     index = index % (uvAnimationTileX * uvAnimationTileY);
    
     // Size of every tile
     var size = Vector2 (1.0 / uvAnimationTileX, 1.0 / uvAnimationTileY);
    
     // split into horizontal and vertical index
     var uIndex = index % uvAnimationTileX;
     var vIndex = index / uvAnimationTileX;
 
     // build offset
     // v coordinate is the bottom of the image in opengl so we need to invert.
     var offset = Vector2 (uIndex * size.x, 1.0 - size.y - vIndex * size.y);
    
     renderer.material.SetTextureOffset ("_MainTex", offset);
     renderer.material.SetTextureScale ("_MainTex", size);
 }



I'm using this texture on gameObject that is instantiated by another. The texture shows a sequence of numbers counting down. The problem I'm having is that the countdown seems to be based on a global time, rather than the amount of time the object has been around for. I could have a hundred of these objects on screen, all generated at different times, but they will all show the same frame of the texture. Is there a way to have the time based off when the object was instantiated, rather than when the program was first run?

I've gone through the forum as well as the script reference and I can't find anything that describes what I'm looking for.

Any help would be greatly appreciated!

Thanks.

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
1
Best Answer

Answer by demize2010 · Jun 10, 2011 at 12:34 AM

Hey man,

You could handle this in multiple ways. Using what you have you could use a boolean that tracks the game state and an offset for the time it actually starts working - for example (remember to add your other variables and code from your first script):

 var gameController : <Game Controller Script Name>;
 var startTime : float;
 
 function Update(){

   if(gameController.countDown){
      if(!startTime){
        startTime = Time.time;
      }

      var index : int = Time.time - startTime * framesPerSecond;

     //The rest of your script
   }    
 
 }

A tidier way to do it would be to have a co-routine running every second which counted just shifted the texture offset incrementally. In this pesudo example you kick of the co-routine by sending the startCountDown message to your countdown object.

 var index : int = 1;

 function startCountDown(){
   InvokeRepeating("countDown", 0, 1); 
 }

 function countDown(){
   if (index < uvAnimationTileX ){
       index ++;
   }
   else{
       index = 1;
   }
   //The rest of your script
 }

EDIT If you are instancing these objects and want them to count from when they are created do this:

 var startTime : float;
 
 function Awake(){
  startTime = Time.time;
 }
 
 function Update(){
 
      var index : int = Time.time - startTime * framesPerSecond;
 
     //The rest of your script
   }    
 
 }

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 OtsegoDoom · Jun 10, 2011 at 11:50 AM 0
Share

Hi, Thanks for the response!

I'm trying out your first suggestion and aside from one error it seems to be working just fine. The first time one of the countdown objects is instantiated the game pauses with an error:

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

If I select the instanced game object (ballCountdown(Clone) it allows me to add an object through the inspected under the Game Controller variable. The only object I can select though is ballCountdown(Clone). If I unpause the game it will run perfectly until the next countdown ball appears, then I have to do this all over again.

The most important part though, is that the textures have the proper offset, so it's almost there.

I'm pretty sure this is the part of the script that's giving me trouble as it's the only part I don't fully understand:


var gameController : destroyAfterTime; var startTime : float;

/ other variables listed here /

function Update(){

if(gameController.countDown){


destroyAfterTime is a script I wrote that will blow up the ball after the countdown has finished. It uses the variable "countDown" to deter$$anonymous$$e how long before the ball detonates.

I think I'm just lost on the term "gameController".

avatar image demize2010 · Jun 10, 2011 at 12:01 PM 0
Share

Hey man, I've edited the answer so you can use this on instanced objects. gameController was a variable for a master control script but I suspect you don't need that.

avatar image OtsegoDoom · Jun 11, 2011 at 12:18 AM 0
Share

It worked! Thank you so much. I actually understand what's going on in your script which I think it the most important part.

Here's the complete script I used in case anyone wants to use it:


var startTime : float;

var uvAnimationTileX = 4; //Here you can place the number of columns of your sheet.

var uvAnimationTileY = 1; //Here you can place the number of rows of your sheet.

var framesPerSecond = 1.0;

function Awake(){ startTime = Time.time; }

function Update(){

 var index : int = Time.time - startTime * framesPerSecond;

 // repeat when exhausting all frames
 index = index % (uvAnimationTileX * uvAnimationTileY);
   
 // Size of every tile
 var size = Vector2 (1.0 / uvAnimationTileX, 1.0 / uvAnimationTileY);
   
 // split into horizontal and vertical index
 var uIndex = index % uvAnimationTileX;
 var vIndex = index / uvAnimationTileX;

 // build offset
 // v coordinate is the bottom of the image in opengl so we need to invert.
 var offset = Vector2 (uIndex * size.x, 1.0 - size.y - vIndex * size.y);
   
 renderer.material.SetTextureOffset ("_$$anonymous$$ainTex", offset);
 renderer.material.SetTextureScale ("_$$anonymous$$ainTex", size);

}


This script will give your object an animated material that will start from the first frame when the object is instantiated.

Thanks again!

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

2 People are following this question.

avatar image avatar image

Related Questions

Problem with time on scrolling texture 1 Answer

Help Guitar Hero Style or similar 0 Answers

Problem with missing texture with Instantiate( Resources.load ) method 0 Answers

How to wait for Start() to run when calling InstantiateObject() 1 Answer

Texture as return type? From instance to prefab. 0 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