Hi,
I am having some issue with Instantiating bullets, making them move then destroying them. I did check the documentation/API and forums and it sounds easy, problem is even when I copy and paste code from documentation I get errors. Got a quick hint from the irc channel that the c# code on the documentation is incorrect, if that is the case it is a bit irritating, but it may be just me (probably doing something really stupid).
This code appears to work, it is a bare bone setup I can get to work, anything more useful that I try I get errors:
using UnityEngine;
using System.Collections;
public class SpawnProjectile : MonoBehaviour {
public Transform projOne;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if (Input.touchCount > 0 Input.GetTouch(0).phase == TouchPhase.Began)
{
DestroyOldProj();
SpawnProjOne();
}
}
void DestroyOldProj()
{
Destroy(projOne.gameObject);
}
void SpawnProjOne()
{
Instantiate(projOne, new Vector3(1.3f, 0.5f, 0f), Quaternion.identity);
}
}
the code above creates an instance of my bullet sphere at the correct coordinates. Destroy old one and creates new one when I touch the iPhone.
Problem/Intention
What I need is for this sphere to travel on the terrain with an inpulse/force and I will want to indeed destroy it when I shoot a new bullet-sphere.
I would actually prefer to create a new variable that will be = Instantiate… etc
so I can do stuff with the newly named clone
Problem is that if I follow the documentation/examples, etc… all I get is errors for example if I follow the code from this Reference Page http://unity3d.com/support/documentation/ScriptReference/Object.Instantiate.html
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);
}
}
}
or any other from here http://unity3d.com/support/documentation/Manual/Instantiating%20Prefabs.html
I get errors of the type
"Cannot implicitly convert type ‘UnityEngine.Object’ to UnityEngine.Rigidbody’. An explicit conversion exists (are you missing a cast?)
I get similar messages if I try to work with Transforms or Rigidbody and any other example I see on docs or forums (even if most forum posts are about UnityScript no c#)
So is there a problem with docs examples, new version of Unity?
What I am trying to do is create a clone-sphere that rolls on the ground in a way that I can access this clone/do things to it (add particles, physics based movement, work with its position, etc…) But I need to get rid of these errors first
Any help/samples/ideas?
Thank you