#Color.Lerp function not workin as intended

1 messages ยท Page 1 of 1 (latest)

hard crag
#

I have this short script to change the color of a sprite renderer that will act as a background in game. However, the sprite renderer is being set and function doing it is being called, but the sprite renderer's color is only the first color of the Color.Lerp. I never used Color.Lerp so the code is probably easily fixable.

using System.Collections.Generic;
using UnityEngine;

public class ChangeColour : MonoBehaviour
{
    SpriteRenderer spriteRenderer;


    // Start is called before the first frame update
    void Start()
    {
        spriteRenderer = GetComponent<SpriteRenderer>();
        ChangeColor(Color.red, Color.blue, 30f);
    }

    // Update is called once per frame
    void Update()
    {
        
    }


    void ChangeColor(Color color1, Color color2, float time)
    {
        Debug.Log("Called");
       
        spriteRenderer.color = Color.Lerp(color1, color2, time );
    }
}
#

I got a NullReffrenceException error

#

but I am not aware on what object

solemn apex
#

Look at the line throwing the exception

hard crag
#

doesnt say

#

not the first time I got that type of a NullReffrenceException error

#

it never shows on what game object it is or anything

#

it just says this

solemn apex
#

This is a Unity internal error

#

You can see that clearly here

#

This is thrown by Unity, not your code

#

You can ignore it

#

If it repeatedly happens, it is likely a bug. Try updating your Unity

hard crag
#

well it does, do you mean update the version or unity hub

#

since im using a 2020 version cus other versions lag

#

and the games im currently working on are in the version

#

I may update it once I finnish the problems

solemn apex
#

See... Unity may be the problem

#

2020 was an awful release

#

It is littered with bugs, things are incomplete. It's pretty universally agreed that 2020 was a pile of shit

hard crag
#

well

#

damn

#

Ig I should finnish my project soon and stop using 2020

rancid silo
#

this is a dumb & useless response, but literally just ask ChatGPT ๐Ÿ’€

#

damn, I stopped using or asking for forums/community responses because of that AI, no more waiting long hours (even days) for literally no answer

#

people just don't have time for forums and aiding others (like me), that's why I really advise you to use ChatGPT, not to do all of your work, but to answer your worst questions. aaand do all of your work.

hard crag
soft coral
#

As an example, ask it to write a save system in C#
I can almost 100% guarantee it'll use the Binary Formatter, which is a massive security issue and will actually be a compliance failure if you try to launch your game on the playstore or app store

#

That being said once you know how to program it can be a useful tool to create simple frameworks or avoid wasting time writing code which has been written 1000x before

lusty oysterBOT
#

If you've used ChatGPT to generate code, it's important to acknowledge the code may not work as intended, or at all. AIs such as these work similarly to predictive text on your phone, but on a much more advanced level - it is simply predicting a response you'd like to hear. They don't understand the nuances of your project, nor your specific requirements.

ChatGPT is a useful tool to help you decide on an overall structure, but blindly copying and pasting code it generates is almost guaranteed to fail. Try solving the problems on your own first, and Google for help before relying on AI.

solemn apex
#

Because this is becoming an increasing problem

#

PSA to Literally Everyone: Stop ๐Ÿ‘ assuming ๐Ÿ‘ ChatGPT ๐Ÿ‘ can ๐Ÿ‘ code ๐Ÿ‘ for ๐Ÿ‘ you.

rancid silo
#

Agree with you guys, though the problem is simply the missuse and overexpectance from those who are learning to code.
Still I like to use ChatGPT like my "super forum searching engine" to seek answers to issues that other people on earth also had.

hard crag
#

I told it to explain it

#

it did, and I wrote the code

#

it gave an example and I wrote something Simmilar that suited my needs

#

seemed fine

worthy inlet
#

Did you check out unity's docs on the function? That's where I would have looked. It's pretty similar to unity's other lerp functions.

#

In fact, it's so similar to the other lerp functions that you can look at tutorials for "unity lerp" in general. Learn how to lerp floating point numbers and/or vectors, and apply those exact same lessons to color values.

hard crag
#

I think I did look at docs aswell

#

and I did not know other lerp functions existed

solemn apex
#

It's just an equation

#

It's nothing but math

#

Tell me - what value is halfway between 10 and 20?

hard crag
#

15

solemn apex
#

Congratulations. You just did this in your head:

Mathf.Lerp(10, 20, 0.5f)
hard crag
#

how

solemn apex
#

That's all Lerp is

#

It returns a value that is some percentage between a start and end

hard crag
#

oh..

solemn apex
#

0.5 being 50%

hard crag
#

well look, I never knew what Lerping even was

solemn apex
#

Now you do WeSmart

hard crag
#

so sure, a tutorial wasn't needed but I just assumed it was some complicated stuff

solemn apex
#

It can get more complicated, if used in certain ways. But Lerp is nothing more than that. It just returns a value that is a percentage between two others.
Color.Lerp does the same thing, it just does Mathf.Lerp on the r, g, b, and a components of the Color

#

Vector3.Lerp is the same, it just does it on x y and z

#
var start = new Vector(1, 10, 100);
var end = new Vector(2, 20, 200);
var result = Vector3.Lerp(start, end, 0.5f);

This will return a vector equal to (1.5f, 15f, 150f)

solemn apex
#

Transitioning from color1 to color2 over time

hard crag
#

oh no..

#

not co routines

solemn apex
#

A coroutine is the easiest and most efficient way to go about it

hard crag
#

wait, the thing I did

#

It just, changed it 1%..