Error code CS0029

I was making a custom column and sfx slider for my game that saves preferences and I got the error code CS0029:
Assets/Scripts/Menu/Music.cs(59,36): error CS0029: Cannot implicitly convert type ‘float’ to ‘UnityEngine.AudioSource’
the code is
using UnityEngine;
using UnityEngine.UI;

public class Music : MonoBehaviour
{
private static readonly string FirstPlay = “FirstPlay”;
private static readonly string BackgroundPref = “BackgroundPref”;
private static readonly string SoundEffectsPref = “SoundEffectsPref”;
private int firstPlayInt;
public Slider backgroundSlider, soundEffectsSlider;
private float backgroundFloat, soundEffectsFloat;
public AudioSource backgroundAudio;
public AudioSource[ ] soundEffectsAudio;

void Start()
{
firstPlayInt = PlayerPrefs.GetInt(FirstPlay);

if(firstPlayInt == 0)
{
backgroundFloat = .25f;
soundEffectsFloat = .75f;
backgroundSlider.value = backgroundFloat;
soundEffectsSlider.value = soundEffectsFloat;
PlayerPrefs.SetFloat(BackgroundPref, backgroundFloat);
PlayerPrefs.SetFloat(SoundEffectsPref, soundEffectsFloat);
PlayerPrefs.SetInt(FirstPlay, -1);
}
else
{
backgroundFloat = PlayerPrefs.GetFloat(BackgroundPref);
backgroundSlider.value = backgroundFloat;
soundEffectsFloat = PlayerPrefs.GetFloat(SoundEffectsPref);
soundEffectsSlider.value = soundEffectsFloat;
}
}

public void SaveSoundSettings ()
{
PlayerPrefs.SetFloat(BackgroundPref, backgroundSlider.value);
PlayerPrefs.SetFloat(SoundEffectsPref, soundEffectsSlider.value);
}

void OnApplicationFocus(bool infocus)
{
if(!infocus)
{
SaveSoundSettings();
}
}

public void UpdateSound()
{
backgroundAudio.volume = backgroundSlider.value;

for(int i = 0; i < soundEffectsAudio.Length; i++)
{
soundEffectsAudio = soundEffectsSlider.value;
}
}
}
if anybody could help I would really appreciate it sorry for any trouble I am new to programming

  • you should have posted this in the Scripting forum, not documentation, it has nothing to do with it
  • please read this topic about how to post code in the forum in order to be readable for others: Using code tags properly
for(int i = 0; i < soundEffectsAudio.Length; i++)
{
soundEffectsAudio = soundEffectsSlider.value;
}

I guess this is the problem. What property you wanted to change?

Hello. I tried to make a score and high score for my game by using Player Prefs but got the error CS0029 in the console.

Here is my code :

using UnityEngine;
using UnityEngine.UI;

public class Score : MonoBehaviour
{
    public Transform player;
    public Text scoreText;
    public Text highscoreText;

    void Start()
    {
        highscoreText.text = PlayerPrefs.GetInt("HighScore", 0).ToString();
    }

    void Update()
    {
        int number = player.position.z.ToString("0");
        scoreText.text = number.ToString();

        if (number > PlayerPrefs.GetInt("HighScore", 0))
        {
            PlayerPrefs.SetInt("HighScore", number);
            highscoreText.text = number.ToString();
        }
    }
}

Here is also the error message:

Assets\Scripts\Score.cs(17,22): error CS0029: Cannot implicitly convert type ‘string’ to ‘int’

Thank you in advance.

Please don’t hijack old threads for new problems. It’s against forum rules. Instead, start your own post… it’s FREE!

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

https://forum.unity.com/threads/assets-mouselook-cs-29-62-error-cs1003-syntax-error-expected.1039702/#post-6730855

Line 17 is explicitly asking for a ToString() but you are assigning it to an integer. That’s the problem.

Beyond that, here is how to report your problem productively in the Unity3D forums:

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

So how exactly can I fix the problem?

Please start a new thread. And to answer the above question, do you want a string or do you want an integer?

I want a string.

Actually, it kinda looks like you want an integer, based on subsequent use of number.

Most likely you want these two lines:

int number = player.position.z.ToString("0");
scoreText.text = number.ToString();

To instead be:

int number = (int)player.position.z;
scoreText.text = number.ToString();

Reasons:

  1. z is a float, you probably only want the integer portion

  2. you are downstream using number to compare to your high score in playerprefs

Thank you it works but when I exit play mode and return in play mode, it doesn’t remember the highscore.

In order to start complying with forum rules, I am not going to reply anymore until you start a new thread for yourself.

This is an entirely new problem.

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:

If you post a code snippet, ALWAYS USE CODE TAGS:

How to use code tags: Using code tags properly

I don’t know how to start a new thread.

Click the ‘Scripting’ bold text at the top of the thread, then in the top right there’s a ‘Start new Thread’ button in a black rectangle.

Ok thank you.

1 Like