- Home /
The question is answered, right answer was accepted
Argument 1: cannot convert from 'int' to 'string'
I know that this is a frequently asked question. I am using int.TryParse to get the GameObjects tag as an int. I googled about the issue and found a lot of answers, But non of them helped me out
This is the code that I am using:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Move_Target : MonoBehaviour
{
public Transform Pointer;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
}
void OnTriggerEnter(Collider other) {
if(other.gameObject.tag == "Player") {
int togo = 0;
int.TryParse(tag, out togo);
Pointer.transform.localPosition = GameObject.Find(togo + 1).tranform.localPosition;
}
}
}
edit: I forgot to mention that int.Parse, int.parse, togo = int.parse(tag) doesn't work too.
Answer by MilkIsTheBest · Dec 15, 2020 at 06:49 AM
My bad. after doing some things I landed on a working code(looks like I was confused about where the error was coming from.
For the nerds who actually wants to check out the code:
using System.Collections; using System.Collections.Generic; using UnityEngine; public class Move_Target : MonoBehaviour { public Transform Pointer; // Start is called before the first frame update void Start() { } // Update is called once per frame void Update() { } void OnTriggerEnter(Collider other) { if(other.gameObject.tag == "Player") { int togo = int.Parse(tag); Pointer.transform.localPosition = GameObject.Find((togo + 1).ToString()).transform.localPosition; } } }
Answer by CmdrZin · Dec 15, 2020 at 07:13 AM
If you're trying to get a unique int for a string, try adding all of your strings to a List then do a myList.IndexOf(string) to return an int.
or
most people just add a public or private int id; variable to an attached script. If you make it static and increment it in Awake, then each object will have a unique Id.
But, if you really want to turn "Player" into an int, you could cast the string into a char array with ToCharArray() and add up the characters or such.