error CS0029: Cannot implicitly convert type `string' to `int'

using UnityEngine;
using UnityEngine.UI;

public class Multijugador : MonoBehaviour

    public string MyIP = "";
    public int MyPort = 1;
    public string HostIp = "";
    public string HostPort = "";
    public int MaxConnections = 8;
    public Text ServerIp;
    public Text ServerPort;
    public Text MaxConnections2;
    public Text Myip2;
    public Text MyPort2;

    void Start()
        MyIP = Network.player.ipAddress;

    void Update()
        HostIp = ServerIp.text;
        HostPort = ServerPort.text;
        Myip2.text = "Tu ip: " + MyIP.ToString();
        MaxConnections = iMaxConnections2.text;    //in this line have error
        MyPort = MyPort2.text;   //in this line have error

    public void Conectar()
        Network.Connect(HostIp, HostPort);

    public void CrearServer()
        Network.InitializeServer(MaxConnections, MyPort, true);

The error says you can’t convert a string to an integer. In your two lines, you’re taking strings and trying to assign them to integer variables, which, as the error message implies, you can’t do.

I’m not certain what you’re trying to do, but don’t forget that when you assign one variable to another, the variable that you want to change goes on the left, and the variable whose value you want to put in the other one goes on the right.

What I try is to create a server but the two fields must be int but in the text it comes out string

have a look at the 2 functions:


if you’re guaranteeing the string is parsable to an int, then you can just use int.Parse.

int.Parse will throw an exception if the text cannot be Parsed where as int.TryParse will return false.

MyPort = int.Parse(MyPort2.text);

int result = 0;
if(int.TryParse(MyPort2.text, out result))
	MyPort = result;