Resources.Load problem

i seem to not be able to get this thing to work, see anything wrong with it (besides none of the variable being declared on this little part)

public void RemoveItem (string _name, float _weight) {
	foreach (Item value in inventory) {
		if (value.name == _name) {
			Debug.Log(_name);
			
			inventory.Remove(value);
			gameObject.GetComponent<MoveInfo>().SpeedMulti += _weight / 100;
			Instantiate(Resources.Load(_name, typeof(Transform)), transform.position + new Vector3(1, 0, 0), transform.rotation);
		}
	}
}

All your resources have to be in a folder called Resources. The Resources folder can have sub folders as well, but will need foldername/ prefix to the filename. No extension is needed.

ok, thank you

That works???

I can have:
resources/images
resources/models
resources/prefabs

and access them as Resources.Load(“images/image1”) ???

Wow! About 2 years ago I was struggling with file paths not matching between Editor and standalone app and finally figured out I can have multiple resource folders and then everything works. Since then I have been using

images/resources
models/resources
prefabs/resources etc

I’ll have to go give this a try because my way frustrated me. I simply thought Unity demanded it that way. I am quite surprised that this might work actually because, as I understand it, at build time Unity takes everything in the resources folder and dumps them into a single folder and creates an asset package from them. I was not aware that they drop folders into the asset folder… I will really have to go test this out! Thanks for the heads up!

hmm, this to be working… GREAT!!! thanks. but one thing, when i call the Instantiate function, it makes it so its called cube(clone) and this then screws up the script (its an inventory script, and it needs to be called cube (an example) the whole way through, any suggestions?)

I had this same issue a while back with my inventory system. I just do a transform.name = RemoveClone(transform.name);

function RemoveClone(from : String) : String
{
	index = from.IndexOf("(Clone)");
	if (index > 0)
	{
		from = from.Substring(0 , index);
	}
	return from;
}

you have a C# version of that?

string RemoveClone(string from)
{
	index = from.IndexOf("(Clone)");
	if (index > 0)
	{
		from = from.Substring(0 , index);
	}
	return from;
}

0.o

.
.
erm, no…

Thanks Dman. It would have taken me ages to do that :stuck_out_tongue:

thanks dan

why don’t you just change the name of the object on instantiation ?

Transform myObj = (Transform) Instantiate(...........);
myObj.name = "objName";

Sometimes you Instantiate one of multiple prefabs, and you need a dynamic way to rename the object.

Alternatively use the trivial way as you don’t care for the index anyway

string RemoveClone(string from)
{
  return from.Replace("(Clone)","");
}

cause above code does not check for clone at the end (which would be done through from.EndsWith(“(Clone)”)), it looks only if its present in the name which above straight replace does as well