Projectile not 'projecting' because i have to use "var xxx : GameObject = Instantiate"

I am using the following code:

        var blaster : GameObject = Instantiate(blasterPrefab, GameObject.Find("blasterPoint").transform.position, Quaternion.identity); 
        // Propells blasted ammunition forward
        blaster.rigidbody.AddForce(transform.forward * 2000);   
        //end fire weapon phase

but the projectile (blaster) does not blast forward since I added "GameObject" as the variable type (needed to do this to allow program to run on iPhone). How do I add the forward momentum to the blasted object when I use the code as I do above?

ScriptableObjects are first class serialized objects. So you have to actually serialize the instance itself. The JsonUtility is quite limited. In the usual serialization system references to ScriptableObjects are serialized as actual references unlike other classes. However JSON doesn't have a concept for references at all.

2 Answers

2

Use blaster.transform.forward.

var blaster : GameObject = Instantiate(blasterPrefab, GameObject.Find("blasterPoint").transform.position, Quaternion.identity); // Propells blasted ammunition forward blaster.transform.forward * 2000; //end fire weapon phase This returns the error "Cannot cast from source type to destination type." ?

I found a work-around:

function Update () {
    transform.LookAt(Clicktomove.blackhole);//blackhole is my target (aka forward direction)
    rigidbody.AddForce(transform.forward * 200);
}

I added the above script to the actual prefab that acts as the projectile itself.

Although that will work, its unneeded. As I said, try using blaster.rigidbody.AddForce(blaster.transform.forward * 2000); Unless you want to keep a constant force for some time, the method I just showed you is cheaper. Every GameObject has a transform.

Using that still gives the error "Cannot cast from source type to destination type." I would love a cheaper alternative that works the way you are attempting to go.