Assignig a game object from within the editor without using find

I wish to assign a game object based on collision, i was essentially creating a empty object and assigning it to the variable then changing at run time but im assuming that you shouldnt need to do that.The basics of it is, you start off with no items inyour hand, you will have to pick one up . so when you collidee with an item you should then be able to pick it up and this assign the selectedTool to the gameObject in question

void OnCollisionStay(Collision Other)
	{
		startTime +=Time.deltaTime;
		
		
		if(startTime >5.0f){
			
			if(Other.collider.tag==("Pickable"))//change mayybe to pickable
			{
				if(Other.collider.gameObject.name =="Scalpal")
				{
					tools.selectedTool=0;
				}
				else if(Other.collider.gameObject.name=="scissors")
				{
					tools.selectedTool=1;
				}
				else{
					tools.selectedTool=2;
				}
				
				
					
				bool isEquipped = CheckTools();//if true then something has been picked up
				selectedTool = Other.collider.gameObject;
				Debug.Log(selectedTool);
				if(lastSelectedTool != null){//so if there was a tool which was last selected
					lastSelectedTool.GetComponent<SelectableUtils>().unselect();//unselect it and give a new value
					lastSelectedTool = selectedTool;
					selectedTool.GetComponent<SelectableUtils>().select(isEquipped);
				}

so at selected = other.colider.gameObject i get an error at runtime saying that the game object is not yet assigned when i actually collide with one of the items.

Are you sure that the item (in this case Other ) game object has a collider component?

Yes, because Other.collider.gameObject.name returns the actual object when i put in in debug

Im able to use if(Other.collider.gameObject.name =="Scalpal") { tools.selectedTool=0; selectedTool =GameObject.Find("Scapal"); and it works fine, so there isnt anything wrong with the collider in that sense, there must be something that im doing wrong with regards to asigning game objects.

You should show the exact error - they're usually pretty self-descriptive.

the error was UnassignedReferenceException: The variable selectedTool of 'SelectTool' has not been assigned. You probably need to assign the selectedTool variable of the SelectTool script in the inspector.

2 Answers

2

hmmm, have you tried Other.transform.gameObject instead?

Ive never really understood what that variable actually does, there is not much documentation about it care to explain
p.s i can confirm that lastSelectedTool = selectedTool.transform.gameObject; works thank you