GetComponent by type and name

how come this works

function addElement (currentTool:String)
{	 
	obj=GameObject.Find(currentTool);
	obj.renderer.material.SetColor("_Emission", Color(1.0,0.0,0.0,0.0));
	var AddName=inventory.PeriodicQuantityTable[current];
	var AddAMT=0;
	AddAMT+=1;
	obj.GetComponent(split).content.Add(AddName,AddAMT);//the name of the script, split,
                                                 // is the same as the string i get with currentTool, it's also the same name as the GO
	drag=false;
}

and so this doesn’t work?
Assets/myAssets/myScripts/lab/cooking.js(73,39): BCE0019: ‘content’ is not a member of ‘UnityEngine.Component’.

function addElement (currentTool:String)
{	 
	obj=GameObject.Find(currentTool);
	obj.renderer.material.SetColor("_Emission", Color(1.0,0.0,0.0,0.0));
	var AddName=inventory.PeriodicQuantityTable[current];
	var AddAMT=0;
	AddAMT+=1;
	obj.GetComponent(currentTool).content.Add(AddName,AddAMT);//should get the same script by name no?
	drag=false;
}

i get currentTool from casting a ray and checking the hit.collider.tag, this way i can add things to alot different GameObjects via the tag i hit and keep track of what they have inside. if it worked… :frowning:

When you use GetComponent with a string, it returns Component rather than the type. But it needs to be cast to the correct type in order to work. I’d suggest looking into SendMessage instead.

–Eric

thanks Eric it worked

function addElement (currentTool:String)
{	 
	obj=GameObject.Find(currentTool);
	obj.renderer.material.SetColor("_Emission", Color(1.0,0.0,0.0,0.0));
	if(disContent=="Molecules")
	{
		var AddName=inventory.MolecularQuantityTable[current];
		obj.SendMessage ("AddToContent", AddName);
		drag=false;
	}