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.
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
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#.
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.
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);
}
}
}