Help please. Trouble accessing a component of an Instance

Hi guys,

Consider the following code…

function OnMouseOver(){
 if(Input.GetMouseButton(1))
  {
   var aCopy : GameObject = Instantiate(GameObject.Find("aTile"), transform.position, transform.rotation);
  }	
}

Now, I am trying to access a “3D Text” object that is a child of aTile and set its Text property but not sure how to do this after I create the instance.

I cant search for the component by name as there maybe other instances?

Basically I want the user to right click a tile object, this creates a duplicate tile at the same position with a 3D text object on it, this tile then flys off up the screen.

Any help please as I’m stumped.

Thanks
Geoff

aCopy is pointing to the instance that you just created, so aCopy.GetComponent(…) should do the trick.

Hi

I tried that initially as:

aCopy.GetComponent(“aLetter”).Text = “DONE”;

But this throws a NullReferenceException error

Also tried:

aCopy.GetComponent(aLetter).Text = “DONE”;

which didnt work either.

Thanks
Geoff

is aLetter a TextMesh object?
try something like this:

aCopy.GetComponent(TextMesh).Text = "DONE";

Yeah, its a Text Mesh but that produces the following error:

MissingFieldException: Field 'UnityEngine.TextMesh.Text' not found.

Thanks anyway
Geoff

OK one last try. the .text should have been lowercase:

aCopy.GetComponent(TextMesh).text = "DONE";

If that doesn’t work, I give up :cry:

That didn’t work, that is trying to access a TextMesh on aTile rather than the child component called Letter.

MissingComponentException: There is no ‘TextMesh’ attached to the “aTile(Clone)” game object, but a script is trying to access it.
You probably need to add a TextMesh to the game object “aTile(Clone)”. Or your script needs to check if the component is attached before using it.

Screen shot attached from a paused state.

Thanks for trying anyway, its appreciated.

Geoff

Hiya,

I can suggest these links for reading on how to access objects/components.

http://unity3d.com/support/documentation/ScriptReference/index.Accessing_Other_Components.html

http://unity3d.com/support/documentation/ScriptReference/index.Accessing_Other_Game_Objects.html

In respects to your current question, I think you want to do something like this in my horrible JS pseeudocode:

var objectToInstantiate : TextMesh;

function OnMouseDown(){
    TextMesh clonedObj = Instantiate(objecttoInstantiate);

clonedObj.text = "Whatever you want it to show.";
}

As mentioned on IRC, here is a modified version in case you wish to do stuff on the parent and want the TextMesh gotten as a child.

Same as before:

var objectToInstantiate : Transform; 

function OnMouseDown(){ 
    Transform clonedObj = Instantiate(objecttoInstantiate); 

    // Get the TextMesh component.
    TextMesh myTextMesh = clonedObj.GetComponentInChildren(TextMesh);
    
    // Check for null else bad things could happen.
    if(myTextMesh != null){
        myTextMesh.text = "Yeeehaw!";
    } else
        Debug.LogError("Could not find TextMesh component.");
}

That done it thanks matey :slight_smile: :slight_smile: :slight_smile:

function OnMouseOver(){
	
	if(Input.GetMouseButton(1)){
	
	var clonedObj : Transform = Instantiate(objectToInstantiate);

    // Get the TextMesh component.
    var myTextMesh : TextMesh = clonedObj.GetComponentInChildren(TextMesh);
   
    // Check for null else bad things could happen.
    if(myTextMesh != null){
        myTextMesh.text = "Yeeehaw!";
    } else
        Debug.LogError("Could not find TextMesh component.");
		//var aCopy : GameObject = Instantiate(GameObject.Find("aTile"), transform.position, transform.rotation);
	}	
}