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

Resolved Error CS0029: Cannot implicitly convert type "string" to "NameScript"(A script class name)

Discussion in 'Scripting' started by KirbGuy, Jun 15, 2023.

  1. KirbGuy

    KirbGuy

    Joined:
    Aug 17, 2021
    Posts:
    3
    I was trying to make a script where it blocks usernames and what I was trying to do before I got this error was I was trying to make it so whenever it detects a bad username it gets a variable from another script class and changes it to a string variable which has the value "**" but I got a compiler error saying: "NameBlock.cs(33,27): error CS0029: Cannot implicitly convert type 'string' to 'NameScript'". I am not too keen at coding right now so I need help with fixing this problem. Heres the full script for my "NameBlock" script:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using TMPro;
    5. using Photon.Pun;
    6. using PlayFab;
    7. using PlayFab.AdminModels;
    8.  
    9. public class NameBlock : MonoBehaviour
    10. {
    11.     public TextMeshPro playerUsername;
    12.  
    13.     public List<string> blockedNames;
    14.     string currentUser;
    15.     string currentUserPhotonPun;
    16.     string blocked = "**";
    17.     public NameScript NameVar;
    18.     public PhotonView PV;
    19.     public uint Duration;
    20.     public string Reason;
    21.     public float BanDelay = 0.01f;
    22.     private bool canBan = true;
    23.  
    24.     // Update is called once per frame
    25.     void Update()
    26.     {
    27.         currentUserPhotonPun = PhotonNetwork.LocalPlayer.NickName;
    28.          currentUser = PlayerPrefs.GetString("Username");
    29.         foreach(string x in blockedNames)
    30.         {
    31.             if(playerUsername.text == x)
    32.             {
    33.                 NameVar = blocked.ToString();
    34.                 playerUsername.text = "";
    35.                 PhotonNetwork.Disconnect();
    36.                 StartCoroutine(BanPlayer());
    37.             }
    38.         }
    39.         foreach(string x in blockedNames)
    40.         {
    41.             if(currentUser == x)
    42.             {
    43.                 PlayerPrefs.SetString("Username", blocked);
    44.             }
    45.         }
    46.          foreach(string x in blockedNames)
    47.         {
    48.             if(currentUserPhotonPun == x)
    49.             {
    50.                 PhotonNetwork.LocalPlayer.NickName = blocked;
    51.             }
    52.         }
    53.     }
    54.  
    55.     IEnumerator BanPlayer()
    56.     {
    57.         canBan = false;
    58.  
    59.         var request = new BanUsersRequest
    60.         {
    61.             Bans = new List<BanRequest>
    62.             {
    63.                 new BanRequest
    64.                 {
    65.                     PlayFabId = GameObject.FindGameObjectWithTag("PlayFab").GetComponent<Playfablogin>().MyPlayFabID,
    66.                     Reason = Reason,
    67.                     DurationInHours = Duration
    68.                 }
    69.             }
    70.         };
    71.         PlayFabAdminAPI.BanUsers(request, (result) =>
    72.         {
    73.             Debug.Log("The Person Has Been Banned");
    74.             Application.Quit();
    75.         }, (error) =>
    76.         {
    77.             Debug.LogError("Error When banning The player: " + error.ErrorMessage);
    78.         });
    79.  
    80.         yield return new WaitForSeconds(BanDelay);
    81.  
    82.         canBan = true;
    83.     }
    84. }
    85.  
    and heres the full code for the script with the names:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5. using Photon.VR;
    6. using TMPro;
    7. public class NameScript : MonoBehaviour
    8. {
    9.     public string NameVar;
    10.     public TextMeshPro NameText;
    11.     private void Update()
    12.     {
    13.         if (NameVar.Length > 12)
    14.         {
    15.             NameVar = NameVar.Substring(0, 12);
    16.         }
    17.         NameText.text = NameVar;
    18.         PhotonVRManager.SetUsername(NameVar);
    19.     }
    20. }
    21.  
     
  2. KirbGuy

    KirbGuy

    Joined:
    Aug 17, 2021
    Posts:
    3
    Please note that I got this script from a tutorial and decided to modifity it how I like it taking inspirations from other scripts.
     
  3. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,718
    Very simple:
    Code (CSharp):
    1. NameVar = blocked.ToString();
    You are trying to assign a string to a variable of type
    NameScript
    .

    When you do an assignment, the type of the variable you are assigning has to match the type of the object you are assigning to it. Examples:

    Code (CSharp):
    1. int x = 5; // OK
    2. int x = "Hello"; // ERROR
    3.  
    4. string example = "Hello"; // OK
    5. string example = 6; // ERROR
    6.  
    7. NameScript ns = GetComponent<NameScript>(); // OK
    8. NameScript ns = "hello"; // ERROR
    In your case you have defined your NameVar variable as such:
    public NameScript NameVar;

    Therefore assigning a string to it will obviously not work. My guess is you meant to write this:
    Code (CSharp):
    1. NameVar.NameVar = blocked.ToString();
    The name "NameVar" for your variable here is very confusing and is probably what's giving you all the trouble to be honest. It would be better if you gave it a better name:
    public NameScript myNameScript;

    then it will be more clear when you write this:
    Code (CSharp):
    1. myNameScript.NameVar = blocked.ToString();
     
    Bunny83, KirbGuy and Ryiah like this.
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,563
    This error is so common you never need to post about it. It's apples and oranges.

    Some help to fix "Cannot implicitly convert type 'Xxxxx' into 'Yyyyy':"

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