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)
{
}
}
}
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
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?)
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
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…
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.
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!