Passing a Game Object to a Function. Need Help

Usually the forums and answers have the…well…answers to keep chugging along. This one though I am completely miffed.

I come from the Neverwinter Nights 1 & 2 scripting/development background. Many things in C# are the same but objects seem to be very different. I have zero issue passing floats, ints, strings into functions. So the syntax of what is required for the object is the issue. The object declaration is compiling fine as well. :slight_smile:

Many NWN functions are incredibly useful and translate great into C#. If anyone can please help understand objects I would be very much appreciative.

NWN Examples:

//Returns TRUE if oObject is a valid object, otherwise FALSE.
int GetIsObjectValid(object oObject);

//Get the data describing an object stored on an object at sVarName.
    object GetLocalObject(object oObject, string sVarName);

//Returns the nearest object to a target by searching for the object’s tag value.
object GetNearestObjectByTag(string sTag, object oTarget = OBJECT_SELF, int nNth = 1);

**This one I can use Find in the hierarchy but I would like to pass an object into the function for consistency.

**My C# translation of a function.
//Stores nValue as a local number within oObject using the variable name sVarName.	
public void SetLocalInt(object oObject, string sVarName, int nValue)
{
  //TODO: Code	
}

When I do this…

public void SetLocalInt(string sObject, string sVarName, int nValue)
**sObject is the Unique ID of a Game Object...searches every game object until finding a match.

I can Find the Object but I am manually pasting in a UID to find it in the hierarchy. So passing the object (or a reference to the object) into the function is a must since this has to be dynamic.

Again. Forward appreciation.

Cheers
Olander

Edited: As suggested for the code

As hinted above…

I decided to attempt a Handshaking type method similar to how Programmable Logic Controllers interact with each other. They are like Game Objects but can only speak to each other through telemetry. This method actually works well in Unity…still cumbersome and not very dynamic but it does work.

So basically if there will be an object the Player Character will need to interact with or keep track of this must be declared public in this method. The GameObject (Cube in this case…or Camp Fire…Enemy) would also need to have any GameObjects public declared. Not too difficult since I have a good idea of what needs to ‘talk’ to whom.

Once these are set I run a simple heartbeat script that has various heartbeats in it that can find the Player and Check that Player’s InstanceID which will always be guaranteed to be unique. When the GameObject’s Heartbeat script Starts it finds an Owner. If the IDs match to whom created…assumption here is not exactly accurate since the Real Owner ‘could’ be father away.

This is where Setting the Actual Owner through a function is the best method…period. No guessing to whom the actual Owner should be.

In any case…here is the code. I will set this as answered.

using UnityEngine;
using System.Collections;

public class gCubeHB : MonoBehaviour 
{
	//Debug Info
	public int nDebug = 1;
	
	public GameObject oSelf;
	public GameObject oOwner;
	public GameObject[] oCheck;
	
	//Game Objects to Get Data
	public gTime gTime;
	public gConversions gConversions;
	public gFunctions gFunctions;
	public gPChb gPChb;
	
	//Variables to Store
	public string sOwner = "";
	public int nPCUID = 0;
	public string sPCUID = "";
		
	//Current Time
	public string sYearCurrent = "";	
	public string sMonthCurrent = "";
	public string sDayCurrent = "";
	public string sDayOfWeek = "";
	public string sHourCurrent = "";
	public string sMinuteCurrent = "";
	public string sSecondCurrent = "";
	public string sTimeCurrent = "";
	
	// Use this for initialization
	void Start () 
	{
		//Get Time Script in _GameData
		gTime = GameObject.Find ("_GameData").GetComponent<gTime>();
		
		//Get Conversion Functions in _GameData
		gConversions = GameObject.Find ("_GameData").GetComponent<gConversions>();
		
		//Get Functions in _GameData
		gFunctions = GameObject.Find ("_GameData").GetComponent<gFunctions>();
		
		//Get Nearest Player...Should be the Creator
		oOwner = GameObject.FindGameObjectWithTag("Player");
		
		//Player Instance ID
		nPCUID = oOwner.GetInstanceID();
		sPCUID = gConversions.IntToString(nPCUID);
		
		//Object Heartbeats
		InvokeRepeating("gHB1Sec", 0, 1.0F);
		InvokeRepeating("gHB5Sec", 0, 5.0F);
	}
	
	//1 Second Heartbeat Items
	void gHB1Sec()
	{
		//Me Test
		oSelf = gameObject;		
		string sSelfName = oSelf.name;
		string sSelfTag = oSelf.tag;
				
		//Current Time from Game Data Object
		gTimeCurrent();		
		
		//Print to Console for Debugging
		if(nDebug == 1)
		{
			print(sPCUID);
			print(sSelfName);
			print(sSecondCurrent);
			print(sTimeCurrent);
		}	
	}
	
	//5 Second Heartbeat Items
	void gHB5Sec()
	{
		print("5 Second HB");
		
		//Find Owner
		oCheck = GameObject.FindGameObjectsWithTag("Player");
		int nCheckUID = 0;
		foreach(GameObject oPC in oCheck)
		{
			nCheckUID = oPC.GetInstanceID();			
			if(nCheckUID == nPCUID)
			{
				//We Have the Owner
				print("My Player Owner Found");
				string sCheckUID = gConversions.IntToString(nCheckUID);
				print("Check UID: "+ sCheckUID);
				break;
			}
		}
		
		//Print to Console for Debugging
		if(nDebug == 1)
		{
			print("5 Second HB");
		}	
	}		
	
	//Time is Updated every 0.5 Seconds
	void gTimeCurrent()
	{
		//Time on PC
		sYearCurrent = gTime.sYearCurrent;
		sMonthCurrent = gTime.sMonthCurrent;
		sDayCurrent = gTime.sDayCurrent;
		sDayOfWeek = gTime.sDayOfWeek;
		sHourCurrent = gTime.sHourCurrent;
		sMinuteCurrent = gTime.sMinuteCurrent;
		sSecondCurrent = gTime.sSecondCurrent;
		sTimeCurrent = gTime.sTimeCurrent;		
	}
}