How to convert 'string' to 'UnityEngine.Transform' | error CS0029

Hey guys!

I got a problem. Unity tells me always to convert the string to UnityEngine.Transform, but I don’t know how to do that.

Here is my code:

using UnityEngine;

using System.Collections;

public class PlayerName : MonoBehaviour {

public string playerName;

private bool iAmOnTheRedTeam = false;

private bool iAmOnTheBlueTeam = false;

private int mynewname;

void Start () 
{
	if(networkView.isMine == true)
	{
		
		GameObject spawnManager = GameObject.Find("SpawnManager");
		
		SpawnScript spawnScript = spawnManager.GetComponent<SpawnScript>();

		GameObject joy_move = GameObject.Find ("Joy_move");

		Joystick joystick = joy_move.GetComponent<Joystick>();

		if(spawnScript.amIOnTheRedTeam == true)
		{

			iAmOnTheRedTeam = true;	
		}
		
		if(spawnScript.amIOnTheBlueTeam == true)
		{

			iAmOnTheBlueTeam = true;	
		}

		if(iAmOnTheBlueTeam == true || iAmOnTheRedTeam == true) {

			mynewname = int.Parse(playerName);

			System.Convert.ToDouble(playerName);

			foreach(PlayerName) {

		

			joystick.PlayerRed = playerName;

		}
	}

	else
	{
		enabled = false;
	}	
}

trying to Access this code:

using UnityEngine;

using System.Collections;

public class Joystick : TouchLogicOld 

{

	 public Transform PlayerRed = null;

}


I want to convert the value of “null” into playerName.

Pls help me!

thanks :smiley:

I pasted your code into a file. I get the first error at line 40:

foreach(PlayerName) {

You are trying to ‘foreach’ a class name, which makes no sense to me.

Then on line 44:

joystick.PlayerRed = playerName;

you are attempting to assign ‘PlayerRed’ (which is a Transform) the string ‘PlayerName’. This will not work and is the error you received.

For future questions, please include the error message copied from the console. It gives us the line number of the error and the stack trace.