- Home /
the specified path is not of a legal form
Hello. I have a script that moves my objects to waypoint:
using UnityEngine;
using System;
using System.Collections;
using System.IO;
public class Move : MonoBehaviour {
public string fileName = "Settings.ini";
Transform[] waypoint;
private float speed;
private float animspeed;
private int currentWaypoint;
private int rnd;
Vector3 velocity;
void Start (){
using (StreamReader fileStream = File.OpenText(Path.GetFullPath(fileName)))
{
string fileLine = "";
while ((fileLine = fileStream.ReadLine()) != null)
{
string option = fileLine.Substring(0, fileLine.IndexOf("="));
switch (option)
{
case "speed":
speed = Convert.ToSingle(fileLine.Substring(fileLine.IndexOf("=") + 1));
break;
case "animspeed":
animspeed = Convert.ToSingle(fileLine.Substring(fileLine.IndexOf("=") + 1));
break;
}
}
fileStream.Close();
}
animation["fly"].speed = animspeed;
waypoint = new Transform[3];
rnd = UnityEngine.Random.Range(1,5);
if(rnd == 1){
currentWaypoint = 0;
waypoint[0] = GameObject.Find("waypointRU1").transform;
waypoint[1] = GameObject.Find("waypointRU2").transform;
waypoint[2] = GameObject.Find("waypointRU3").transform;
}
if(rnd == 2){
currentWaypoint = 0;
waypoint[0] = GameObject.Find("waypointRD1").transform;
waypoint[1] = GameObject.Find("waypointRD2").transform;
waypoint[2] = GameObject.Find("waypointRD3").transform;
}
if(rnd == 3){
currentWaypoint = 0;
waypoint[0] = GameObject.Find("waypointLU1").transform;
waypoint[1] = GameObject.Find("waypointLU2").transform;
waypoint[2] = GameObject.Find("waypointLU3").transform;
}
if(rnd == 4){
currentWaypoint = 0;
waypoint[0] = GameObject.Find("waypointLD1").transform;
waypoint[1] = GameObject.Find("waypointLD2").transform;
waypoint[2] = GameObject.Find("waypointLD3").transform;
}
}
void Update (){
if(currentWaypoint < waypoint.Length){
Vector3 target = waypoint[currentWaypoint].position;
Vector3 moveDirection = target - transform.position;
velocity = rigidbody.velocity;
if(moveDirection.magnitude < 1){
currentWaypoint++;
}
else{
velocity = moveDirection.normalized*speed;
}
}
rigidbody.velocity = velocity;
}
void LateUpdate (){
Vector3 target = waypoint[currentWaypoint].position;
transform.LookAt(target);
}
}
In the Editor its work fine, but when i buid and run my game i have an error, something like this: the specified path is not of a legal form. How fix that?
Answer by Dave-Carlile · Feb 14, 2013 at 12:49 PM
Look in the folder where you're running the game. Does settings.ini
exist in the same folder as the executable? If settings.ini
isn't part of your Unity
project, it won't get copied to the final build.
After looking at your log file, your filename variable is empty...
public string fileName = "Settings.ini";
Public properties like this show in the inspector and allow you to edit them there. The value in the inspector always overrides any initialization you do when declaring the variable. If you look at this value in the inspector, what does it show?
Yes it exists. Log says that i have error in this line: if(currentWaypoint < waypoint.Length){ and this: Vector3 target = waypoint[currentWaypoint].position;
But when i not use stream reader ( didnt take parametr from file) its work.
The error you mention would be caused by file IO.
File.OpenText(Path.GetFullPath(fileName))
Before that line, add Debug.Log
calls to display fileName
, and the resulit of the GetFullPath
call...
Debug.Log("filename=" + fileName);
Debug.Log("fullpath=" + Path.GetFullPath(fileName));
See what that shows.
I have next errors in log:
filename=
UnityEngine.Debug:Internal_Log(Int32, String, Object)
UnityEngine.Debug:Log(Object)
$$anonymous$$ove:Start() (at C:\BatGame\Assets\Scripts\$$anonymous$$ove.cs:17)
(Filename: C Line: 0)
ArgumentException: The specified path is not of a legal form (empty).
at System.IO.Path.InsecureGetFullPath (System.String path) [0x0002c] in /Applications/buildAgent/work/3df08680c6f85295/mcs/class/corlib/System.IO/Path.cs:336
at System.IO.Path.GetFullPath (System.String path) [0x00000] in /Applications/buildAgent/work/3df08680c6f85295/mcs/class/corlib/System.IO/Path.cs:289
at $$anonymous$$ove.Start () [0x00015] in C:\BatGame\Assets\Scripts\$$anonymous$$ove.cs:18
(Filename: C Line: 0)
NullReferenceException: Object reference not set to an instance of an object
at $$anonymous$$ove.LateUpdate () [0x00000] in C:\BatGame\Assets\Scripts\$$anonymous$$ove.cs:100
(Filename: C Line: 0)
Notice how your fileName variable is empty - it has no value. I updated my answer with what is likely causing the issue.