Instantiation Problem

Well I have a feeling there’s something really simple I’m doing wrong here, but I’m trying to Instantiate something like

Transform clone;
clone = Intstantiate(projectile,transform.position,direction);

but for some reason I keep getting an error

Cannot implicitly convert type UnityEngine.Object' to UnityEngine.Rigidbody’. An explicit conversion exists (are you missing a cast?)

I’m using C++, can anyone help? Thanks :smile:

I’m guessing projectile is a member variable that you are typing as a Rigidbody, and then you’re trying to instantiate a Rigidbody as a Transform and it’s yelling at you. Either make clone a Rigidbody or make projectile a Transform.

The projectile is a Transform, it’s not really a projectile i just used that because it was generic i guess

Strange. whats the rest of the script look like? Specifically the line where you declare projectile?

Also, you’re coding in C#, not C++ :wink:

That’s the code, btw I keep getting C++ and C# mixed up XD

And what’s the error your getting? I can’t tell if you made that Rigidbody declaration because of my last post or not, but it looks like Rigidbody portal; should be Transform portal; now.

error CS0266: Cannot implicitly convert type UnityEngine.Object' to UnityEngine.Transform’. An explicit conversion exists (are you missing a cast?)
I changed the portal into a Transform portal and i still get the Error

Your Instantiate function does not return a Rigidbody… Originally it returns a GameObject. To make it do this you will need to write it like this:

				Transform portal;
				portal = Instantiate(portalUsed, hit.point,
					Quaternion.FromToRotation(Vector3.up,hit.normal)).transform;
				portal.position += new Vector3(0,0.1F,0);
				portal.gameObject.SendMessage("WallInput",hit.transform);

May I suggest some coding differences to save processor usage…

using UnityEngine;
using System.Collections;

public class PortalGun : MonoBehaviour {
	public float range = 100F;
	public float fireSpeed = 0.1F;
	public Transform bluePortal;
	public Transform orangePortal;
	public LayerMask layersMasked = -1;

	private float lastShot = 0.0F;
	private Transform portalUsed;
	public Transform tPortal;
	
	void Start(){
		// Place bluePortal and orangePortal in the scene, not prefabs.
		bluePortal.renderer.enabled=false;
		orangePortal.renderer.enabled=false;
		portalUsed=orangePortal;
	}
	
	void Update(){
		if(Input.GetButtonDown("Fire1")){Fire1();}
		if(Input.GetButtonDown("Fire2")){Fire2();}
		
	}
	
	void Fire ()
	{
		if(lastShot + fireSpeed < Time.time)
		{
			// since objects are retained in the scene, we simply choose the inactive one.
			if(portalUsed == bluePortal){
				portalUsed=orangePortal;
			}else{
				portalUsed=bluePortal;
			}
			// enable the one we are using.
			portalUsed.renderer.enabled=false;
			
			RaycastHit hit;
			// this is always in the z direction, this is not associated with the player's direction
			//Vector3 direction = transform.TransformDirection(0,0,1);
			// this is a better use of the direction.
			Vector3 direction = transform.forward;
			lastShot = Time.time;
			if(Physics.Raycast(transform.position,direction,out hit,range,layersMasked))
			{
				// Instantiate returns a GameObject, use it since we are removing the transform later.
				GameObject portal;
				// adding the hit.normal to the instantiate will save the move.
				portal = Instantiate(portalUsed, hit.point + hit.normal * 0.1F,
					Quaternion.FromToRotation(Vector3.up,hit.normal));
				portal.SendMessage("WallInput",hit.transform);
			}
		}
	}
}

In C# Instantiate() returns a UnityEngine.Object as your error is saying. This is a generic object that all unity objects are. All you need is ‘as Object you are Instantiating’ on the end of that line.

like this:

GameObject newInstance = Instantiate(oldGameObject) as GameObject;

or

Transform newInstance = Instantiate(oldTransform) as Transform;

Generally when you get a message saying “(are you missing a cast?)”, you are probably missing a cast and like I’ve shown you above ‘as’ is how you cast in C#.

Thanks guys I’ll try that! Thanks for teaching me about casting :smile:

I don’t code in C# too often, but I thought this too… however I didn’t suggest it because they don’t do this in the documentation example in C#:

using UnityEngine;
using System.Collections;

public class example : MonoBehaviour {
    public Transform prefab;
    void Awake() {
        int i = 0;
        while (i < 10) {
            Instantiate(prefab, new Vector3(i * 2.0F, 0, 0), Quaternion.identity);
            i++;
        }
    }
}

They don’t do it there because they aren’t assigning the result of Instantiate() to a local variable for further manipulation. Instantiate() on it’s own will put the new object into the scene and let it go about its business. :slight_smile:

What about the second example, though?

using UnityEngine;
using System.Collections;

public class example : MonoBehaviour {

	public Rigidbody projectile;
	
	void Update() {
		if (Input.GetButtonDown("Fire1")) {
			Rigidbody clone;
			clone = Instantiate(projectile, transform.position, transform.rotation);
			clone.velocity = transform.TransformDirection(Vector3.forward * 10);
		}
	}
	
}

Oh, I guess that’s just fail?

Except it compiles and works! :face_with_spiral_eyes:

I’m confused also.:face_with_spiral_eyes: