Player Inventory problem

I’ve got a bit of bother with a player’s inventory. I need to have the player pick up a welder, then hod it in his hand. To do that I Instantiate the welder, then try and parent it to a grab point. However, to instantiate it it needs to be a gameobject, but to parent it, it need to be a tansform. Is there any way I can switch the two halfway, or at least get the welder to follow the grabpoint around?

Here’s the code;

using UnityEngine;
using System.Collections;

public class PlayerInventory : MonoBehaviour {
	public Transform grenade; //this is the actual grenade the player will hold
	public Transform welder; //this is the actual welder the player will hold
	private bool hasGrenade; // nuff said
	private bool hasWelder; //ditto
	public GameObject pickupWelder; // this is the pickup of the welder
	public Transform grabPoint;
	private Transform clonedWelder;
	
	void Start()
	{
		hasWelder = false;
		pickupWelder = GameObject.FindWithTag("Welder");
	}
	
	void OnTriggerEnter(Collider other)
	{
		if(other.tag == "Welder")
		{
			hasWelder = true;
			Instantiate (welder, grabPoint.position, grabPoint.rotation);
			//need to be able to parent the welder to the grabPoint here, or at least get it to follow it around.
			Destroy(pickupWelder);
		}
	}
	
	void Update()
	{
		if (hasWelder == true)
		{
		}
	}

}

Hi, Store the Instantiated gameObject in a temporary variable.

GameObject g = Instantiate (welder, grabPoint.position, grabPoint.rotation);
g.transform.parent = grabPoint;
g.transform.localPostion = Vector3.zero;

Or you can have the welder as a child object in the scene but only activate it when equipped.

That might have worked, but I’m planning on using this as a script for an MMORPG (Eventually :P), so It would kind of be untidy if I had all those items in the scene. I’ll still try it out, though :slight_smile:

Hi,

Unfortunately it just comes up with this error;

Assets/Robot game Scripts/PlayerInventory.cs(24,36): error CS0266: Cannot implicitly convert type UnityEngine.Object' to UnityEngine.GameObject’. An explicit conversion exists (are you missing a cast?)

…When I put it like this;

	void OnTriggerEnter(Collider other)
	{
		if(other.tag == "Welder")
		{
			hasWelder = true;
			GameObject g = Instantiate (welder, grabPoint.position, grabPoint.rotation);
			g.transform.parent = grabPoint;
			g.transform.localPosition = Vector3.zero;
			Destroy(pickupWelder);
		}
	}

Sorry, Instantiate returns a generic base object. You need to typecast the result into the target variable.

	void OnTriggerEnter(Collider other)
	{
		if(other.tag == "Welder")
		{
			hasWelder = true;
			GameObject g = (GameObject)Instantiate (welder, grabPoint.position, grabPoint.rotation);
			g.transform.parent = grabPoint;
			g.transform.localPosition = Vector3.zero;
			Destroy(pickupWelder);
		}
	}

Also, I would add a boolean check to see if hasWelder is true before you instantiate, If you are planning on an “MMORPG” you would likely pick up the same type of object multiple times, but if you already have one, in your hand, no need to Instantiate again:

	void OnTriggerEnter(Collider other)
	{

		if(other.tag == "Welder"  !hasWelder) // If hasWelder is false and collided with a Welder, allow an instantiation.
		{
			hasWelder = true;
			GameObject g = (GameObject)Instantiate (welder, grabPoint.position, grabPoint.rotation);
			g.transform.parent = grabPoint;
			g.transform.localPosition = Vector3.zero;
			Destroy(pickupWelder);
		}
            
	}

Thanks. Yeah, I added a boolean but didn’t use it. I’m kind of having to take it one step at a time :stuck_out_tongue: :slight_smile:

I tried it, and it ran smoothly until;

InvalidCastException: Cannot cast from source type to destination type.
PlayerInventory.OnTriggerEnter (UnityEngine.Collider other) (at Assets/Robot game Scripts/PlayerInventory.cs:24)

It instantiated it, but didn’t parent it to the grabpoint.

Cast… What’s that? Does it mean the swapping of one to another? If so I could try to search that on the reference…

Umm… Any help/ :P:face_with_spiral_eyes:

Hi, like I said, “You need to typecast the result into the target variable.”. Your grabpoint is of type Transform. I wrongly suggested to typecast as GameObject when you should have used Transform.

When Instantiating an object and storing it somewhere, the object you are instantiating must be the same type as the variable you are trying to store it in.

Transform g = (Transform)Instantiate (welder, grabPoint.position, grabPoint.rotation);

The reason this works is because Instantiate returns an object of type “object”. Which is the base class for all Unity object.

For info on what TypeCasting is:

http://msdn.microsoft.com/en-us/library/ms173105.aspx

http://www.codeproject.com/Articles/447634/A-Beginners-Tutorial-Type-Casting-and-Type-Convers

Aaaah… So if I put if (object.name == “someName”) it would return if it was a Transform or GameObject? Hmmm, that’s rally useful, as I was using tags all the time…That’s loads for that! :slight_smile:

No, that is not true.

if(object.name == “someName”) is just an expression which evaulates to either true or false. It doesn’t return anything.

:facepalm: Yeah, stupid me :stuck_out_tongue: I was mind numbed at the time of posting :stuck_out_tongue: Too much lerpzing :stuck_out_tongue: Anyway, yeah I see now.:slight_smile: