Change a tag of a Object based on raycast hit (js)

HI guys (sorry my english) but i have some objects with this:

Object 1 => HoldTagMessage.js attach

var holdTagInfo : String;
    
    function Start () {
    	holdTagInfo = this.gameObject.transform.tag;
    	print ("esse objeto tem uma tag que eh: " + holdTagInfo);
    }

Object 2 => RayCastEnemy.js attach

private var novaTag : HoldTagMessage;

function Start () {
	novaTag = new HoldTagMessage();  // i got a error here, say to use AddComponent
}

function Update () {

	// a lot of code here, but it doesnt matter because is fine until here...
	if (foundHit) // that condition is true so the problem is inside
	{
		novaTag.GetComponent(HoldTagMessage).transform.tag = "lockEnemy";
	}
}

So… i need to change the tag of Object 1 (that was “enemy”) to “lockEnemy”. The ‘foundHit’ is a raycast that’s work… but whats inside (change the tag) is wrong… What is the way to fix that… regards everyone.

You can’t use the new keyword to create objects that drive from Monobehaviour. To create them, you need to use AddComponent(), which is a method GameObjects have. Your HoldTagMessage script Will run Start() on the frame after its created. I’d suggest using a different method name and manually calling it in your other script. But really you could just be doing all this in one script…

oldTag = gameObject.tag;
gameObject.tag = someString;

Almost. I delete the script of the Object 1 (i dont use anymore), so on Object 2 i have RayCastEnemy.js attach like this:

var tagCheck : String = "enemy";

function Update () {
	var foundHit : boolean = false;
	var hit : RaycastHit;

	if (Input.GetButton("Fire1"))
	{
	  var fwd =  transform.TransformDirection (Vector3.forward);
	  foundHit = Physics.Raycast(transform.position, fwd, hit);
	}
	
	if (foundHit  && hit.transform.tag != tagCheck)
	  foundHit = false;

	if (foundHit)
	{
	  gameObject.tag = "lockEnemy";
	}
}

but naturaly i change the tag off my cannon (that have this js file attach) and not the tag of the ‘enemy’ (another gameobject)… so, sorry i still not have the result.