Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

What Am I Doing Wrong Here?

Discussion in 'Scripting' started by JWdell, Sep 19, 2021.

Thread Status:
Not open for further replies.
  1. JWdell

    JWdell

    Joined:
    Oct 24, 2014
    Posts:
    54
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class TextureAnimate : MonoBehaviour {
    6.  
    7.     public Texture2D[] frames;
    8.     public int framesPerSecond = 10;
    9.     private Renderer _renderer;
    10.  
    11.     void OnEnable()
    12.     {
    13.         _renderer = GetComponent<Renderer>();
    14.     }
    15.  
    16.     void Update()
    17.     {
    18.         int index = Time.time * framesPerSecond;
    19.         index = index % frames.Length;
    20.         _renderer.material.mainTexture = frames[index];
    21.     }
    22. }
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,756
    What you are doing is failing to explain what is wrong.

    I can see right away you have compiler errors. Go fix them.

    Remember: NOBODY memorizes error codes. The error code is absolutely the least useful part of the error. It serves no purpose at all. Forget the error code. Put it out of your mind.

    The complete error message contains everything you need to know to fix the error yourself.

    Always start with the FIRST error in the console window, as sometimes that error causes or compounds some or all of the subsequent errors.

    The important parts of the error message are:

    - the description of the error itself (google this; you are NEVER the first one!)
    - the file it occurred in (critical!)
    - the line number and character position (the two numbers in parentheses)
    - also possibly useful is the stack trace (all the lines of text in the lower console window)

    All of that information is in the actual error message and you must pay attention to it. Learn how to identify it instantly so you don't have to stop your progress and fiddle around with the forum.


    How to report your problem productively in the Unity3D forums:

    http://plbm.com/?p=220

    How to understand compiler and other errors and even fix them yourself:

    https://forum.unity.com/threads/ass...3-syntax-error-expected.1039702/#post-6730855
     
    bobisgod234 likes this.
  3. JWdell

    JWdell

    Joined:
    Oct 24, 2014
    Posts:
    54
    Sorry, not helpful. If you can see what's wrong just say so. I've tried everything the compiler error says to change and it doesn't work. Can't change int to float, can't change float to int so I haven't a clue to to fix the problem.
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,756
    Bunny83 likes this.
  5. bobisgod234

    bobisgod234

    Joined:
    Nov 15, 2016
    Posts:
    1,042
    Help us help you and paste the actual error. We are not human compilers, and we can't read minds.

    Don't make helping you difficult.
     
    Bunny83 likes this.
  6. JWdell

    JWdell

    Joined:
    Oct 24, 2014
    Posts:
    54
    I thought I did in my last post.

    Here's the exact error "Cannot implicitly convert type `float' to `int'.

    Sorry, I'm coming from knowing Javascript. C# is kind of new to me so sorry if I don't fully understand the syntax on how to change these things. What I'm not understanding is, I'm using Int, not Float in the C# script so I don't understand the error unless the error has to do with Time.time using Float in its functionality. The first line in Update is the compiler complaint. Not matter which I use, Int or Float it will always give that error.

    The Javascript way of doing it isn't a problem and works 100%.

    Code (CSharp):
    1. var frames : Texture2D[];
    2. var framesPerSecond = 10.0;
    3. private var _renderer : Renderer;
    4.  
    5. function OnEnable()
    6. {
    7.     _renderer = GetComponent.<Renderer>();
    8. }
    9.  
    10. function Update()
    11. {
    12.     var index : int = Time.time * framesPerSecond;
    13.     index = index % frames.Length;
    14.     _renderer.material.mainTexture = frames[index];
    15. }
     
  7. bobisgod234

    bobisgod234

    Joined:
    Nov 15, 2016
    Posts:
    1,042
    The full error contains the line the error is on. By crudely paraphrasing the error like you did in your post, you removed this important piece of information.

    My guess is that the error was on this line:

    Code (CSharp):
    1.         int index = Time.time * framesPerSecond;
    2.  
    TIme.time is a float, and framesPerSecond is an int. When you multiply a float with an int, the result is a float. You than try and assign the result to an int variable. In C#, you cannot assign a float to an int as there is a loss of information; int's are less precise than floats. You need to manually typecast the result to an int.

    Code (CSharp):
    1.         int index = (int)(Time.time * framesPerSecond);
    2.  
     
    Bunny83 and JWdell like this.
  8. zombiegorilla

    zombiegorilla

    Moderator

    Joined:
    May 8, 2012
    Posts:
    8,968
    Please read the rules. Put some effort into your posts. Include all information to help you, also include what you have already have tried and what is not working as expected. Simply post "Halp! Broke!" and some code is against our rules of conduct. Closed.
     
    Bunny83 likes this.
Thread Status:
Not open for further replies.