Referencing variables from a different object [HELP]

Ok, I’ve been searching for a while. Nothing really seems to fit what I’m looking for.

I have an indicator object that is placed in front of the player’s character. The indicator displays two rays that check for ground in front of the player. If the ray hits a ground object it gives a value to a variable. I need to move this variable from this object’s script to my player character’s script. I think GetComponent would work but I just don’t know how to use it.

The documentation isn’t making sense to me.

It’s hard to explain…So really just start throwing out a few examples on referencing variables from a different object’s script. I’m at the end of my ropes on this one. I’ve got everything working except referencing variables between the scripts. … … ;lsadjf;aljfoeinfa;lsdv…:rage:

Hello LoInut

Very simple in fact.

assuming “ManageTouch” is a script attache to object GO1, you would simple “get the component” from this object to a variable and use this variable as an object :

ManageTouch ctrlScriptCube;
ctrlScriptCube = GO1.GetComponent<ManageTouch>();
if(ctrlScriptCube)
{
	ctrlScriptCube.DoSomeThing();
}

You have of course to ensure the GetComponent returns something.

Hope this help.

hcia

Ok. Im going to comment out the code so we all know the parts I don’t understand because I don’t deny that this will work its just I don’t know how to use it.

//Is this first line in its proper syntax? What does it do?
ManageTouch  ctrlScriptCube;

//ctrlScriptCube is set to GO1.GetComponent(ScriptName, in this case ManageTouch)
ctrlScriptCube = GO1.GetComponent(ManageTouch);

//Is the if statement needed? If it retrieves something Itll return true...I guess this is to prevent null errors, etc...
if(ctrlScriptCube)
{

              //Ok so here I get lost real quick. What does this line do!?! I never got 
              //What it meant. is it calling the variable? or is it calling a function(which it appears to be);
              ctrlScriptCube.DoSomeThing();
}

//In summary what will this take from my reference object, and add to my target object.
private var canPlace : boolean;
var rHitChk : IndicatorScript;
var lHitChk : IndicatorScript;

rHitChk = GetComponent(IndicatorScript);
lHitChk = GetComponent(IndicatorScript);
	
	if((rHitChk.rHit == true  lHitChk.lHit == true) || (rHitChk.rHit == false  lHitChk.lHit == false))
	{
		print("can place = True");
		canPlace = true;
		return;
	}
	if((rHitChk.rHit == false  lHitChk.lHit == true) || (rHitChk.rHit == true  lHitChk.lHit == false))
	{
		print("can place = false");
		canPlace = false;
		return;
	}

The above code is the section in my character that needs to reference the variable. It checks to make sure that the rays are either both hitting something (true true) or over empty space (false false).

var rHit : boolean;
var lHit : boolean;
var dist : float = 0.1;
private var hitInfoR : RaycastHit;
private var hitInfoL : RaycastHit;

if(Physics.Raycast(transform.position + Vector3(.5,-.5,0),Vector3(0,-1,0),hitInfoR,dist))
{
	print("rhit = true");
	rHit = true;
}
if(!Physics.Raycast(transform.position + Vector3(.5,-.5,0),Vector3(0,-1,0),hitInfoR,dist))
{
	print("rhit = false");
	rHit = false;
}
if(Physics.Raycast(transform.position + Vector3(-.5,-.5,0),Vector3(0,-1,0),hitInfoL,dist))
{
	print("lhit = true");
	lHit = true;
}
if(!Physics.Raycast(transform.position + Vector3(-.5,-.5,0),Vector3(0,-1,0),hitInfoL,dist))
{
	print("lhit = false");
	lHit = false;

The above code is from the Indicator’s Script. It has the rays. Trust me this part works…at least the print does. It is detecting correctly as far as I can tell from what it is printing to my console.

so as one can see, My grasp of the whole GetComponent concept is seriously lacking.

all of these exist in the Update function.

//Is this first line in its proper syntax? What does it do?
ManageTouch  ctrlScriptCube;

Yes. In Unity you can declare a variable of a specific script type. In this case, you are declaring a ManageTouch variable called ctrlScriptCube.

//ctrlScriptCube is set to GO1.GetComponent(ScriptName), in this case ManageTouch)
ctrlScriptCube = GO1.GetComponent(ManageTouch);

Yes, thats correct. This assumes you only using this script once on the object calling the GetComponent. If you where using multiple instances of the same script on object, GetComponent(ScriptName) would return the 1st one it finds. If the component is on another gameobject, you’d need to find that objected first before using GetComponent.

//Is the if statement needed? If it retrieves something Itll return true...I guess this is to prevent null errors, etc...
if(ctrlScriptCube)
{

Like you said, it’s simply a null reference error catch.

              //Ok so here I get lost real quick. What does this line do!?! I never got 
              //What it meant. is it calling the variable? or is it calling a function(which it appears to be);
              ctrlScriptCube.DoSomeThing();
}

This is just dummy code, simply indicating the point where you can now use your script reference (call a fundtion, acess a varaible etc.)

//In summary what will this take from my reference object, and add to my target object.

This is just to get a reference of one script into another, from there you can acess variables, call functions etc

Of course, you can just declare the script type variable in code and manually link it in the editor, but generally speaking the code way is cleaner.

Hope that helps!

Character Script

var HitChk : IndicatorScript;
var canPlace : boolean;
function Update()
{
        HitChk = starBurst.GetComponent(IndicatorScript);
	var RHit : boolean;
	var LHit : boolean;
	RHit = HitChk.rHit;
	print(RHit);
	LHit = HitChk.lHit;
	print(LHit);
	if((RHit  LHit) || (!RHit  !LHit))
	{
		print("can place = True");
		canPlace = true;
	}
	if((!RHit  LHit) || (RHit  !LHit))
	{
		print("can place = false");
		canPlace = false;
	}
}

Indicator Script

//Draw both Rays at Edges
//Check Contact Info
var dist : float = .1;
private var hitInfoR : RaycastHit;
private var hitInfoL : RaycastHit;
static var rHit : boolean;
static var lHit : boolean;

function Update()
{
	if(!Physics.Raycast(transform.position + Vector3(.5,-.5,0),Vector3(0,-1,0),hitInfoR,dist))
	{
		rHit = false;
	}
	else
	{
		rHit = true;
	}
	if(!Physics.Raycast(transform.position + Vector3(-.5,-.5,0),Vector3(0,-1,0),hitInfoL,dist))
	{
		lHit = false;
	}
	else
	{
		lHit = true;
	}
}

Ok so the variables arn’t being sent to the character. The above code is copy pasted from the game. I don’t know why the variables arn’t being referenced. I am using something wrong, but since I still don’t know how to use the GetComponent correctly I don’t know what it is I’m doing wrong.

EDIT: Ok so upon further, mess-aging around. It would appear the two scripts are being referenced. Whenever I make the Indicator a child of the character, the values just stop printing. So it stops updating the variables when I move the Indicator. I’m still grinding away at this. If anyone has some helpful advice, I would give them a digital hug.

Ok…I feel like a complete idiot right now. I solved the problem… Note to self: return; kicks the script out.

So basically I had a return; placed further up in the Update function that whenever I picked up a block it would activate and stop the script. So now everything works correctly. Thank you everyone for the help with understanding GetComponent more thoroughly.

In Indicator

var dist : float = .1;
private var hitInfoR : RaycastHit;
private var hitInfoL : RaycastHit;
var rHit : boolean;
var lHit : boolean;
var CanPlace : boolean;

function Update()
{
	if(!Physics.Raycast(transform.position + Vector3(.5,-.5,0),Vector3(0,-1,0),hitInfoR,dist))
	{
		rHit = false;
	}
	else
	{
		rHit = true;
	}
	if(!Physics.Raycast(transform.position + Vector3(-.5,-.5,0),Vector3(0,-1,0),hitInfoL,dist))
	{
		lHit = false;
	}
	else
	{
		lHit = true;
	}
	
	if(rHit == true  lHit == true || rHit == false  lHit == false)
	{
		CanPlace = true;
	}
	else
	{
		CanPlace = false;
	}
}

In Character

...
var canPlace : boolean;
var HitChk : IndicatorScript;
function Update()
{
...

HitChk = starBurst.GetComponent(IndicatorScript);
canPlace = HitChk.CanPlace;
}

Final Code (relevant) code for anybody who has a similar issue to myself. (Hopefully not involving return;