- Home /
adding script to a clone
I've got a missile script that uses a few other scripts (like an explosion and MoveObject script), but I can't figure out how to add, access, and use them through this script. Take a look at my code, what do you think I'm doing wrong?
p.s. don't question the use of constantforce here, it's just for realism.
Condensed Version (the main problem areas--just ask if you want to see the rest)
//needed to access script MoveObject, it must be attached to a GO
var emptyGO : GameObject;
var moveObject : MoveObject = emptyGO.AddComponent(MoveObject);
//needed to add explosionPrefab to the missile
public var explosionPrefab : Transform;
var missileCloneScript = instantiatedProjectile.AddComponent(MissileClone);
missileCloneScript.explosionPrefab = explosionPrefab;
//this is what I'm trying to use for movement but I can't figure out how to access the script through mine
if (seek)
yield moveObject.use.Translation(instantiatedProjectile.transform, target.position, 2.5, MoveType.Speed);
Full Version (comments provide some more detail)
#pragma strict
var projectile : GameObject; //the actual GO of the missile, whose rigidbody is used for
//instantiatedProjectile, and then is destroyed once launched.
//Pod, used for the start location of instantiatedProjectile. don't really need.
var Pod : Transform;
//boolean used for the firing of missile
private var missilefire : boolean = true;
//boolean used for the seeking action of missile
private var seek : boolean = false;
//instantiated missile clone
private var instantiatedProjectile : GameObject;
//needed to access script MoveObject, it must be attached to a GO
var emptyGO : GameObject;
var moveObject : MoveObject = emptyGO.AddComponent(MoveObject);
//needed to add explosionPrefab to the missile
public var explosionPrefab : Transform;
var missileCloneScript = instantiatedProjectile.AddComponent(MissileClone);
missileCloneScript.explosionPrefab = explosionPrefab;
var target : Transform; //miscellaneous variables
var smooth = 0.1;
var reloadTime = 0.5;
var ammoCount = 8;
var lastShot = -10.0;
if (seek)
yield moveObject.use.Translation(instantiatedProjectile.transform, target.position, 2.5, MoveType.Speed);
// ^this is what I'm trying to use but I can't figure out how to access the script
function Update () {
//stopping the y-motion effect that gravity created
if (instantiatedProjectile.rigidbody.velocity.z > 10)
{
instantiatedProjectile.rigidbody.constantForce.force = Vector3(0,0,0);
instantiatedProjectile.rigidbody.velocity.y = 0;
seek = true;
}
}
function FixedUpdate () {
//missile tracking process
if (seek)
{
//instantiatedProjectile.transform.position = Vector3.MoveTowards (instantiatedProjectile.transform.position, target.position,Time.deltaTime * 5);
// ^ This worked, but when the missile got about 10 units close to the target,
// it seemed to repel and started moving away.
instantiatedProjectile.transform.LookAt(target);
}
}
function Fire ()
{
if (Time.time > reloadTime + lastShot && ammoCount > 0)
{
//creating a firing sequence, instantiating clones, and destroying GOs/adding gravity for realism
if (missilefire)
{
instantiatedProjectile = Instantiate (projectile, Pod.position, transform.rotation);
Destroy (projectile);
instantiatedProjectile.rigidbody.useGravity = true;
yield WaitForSeconds(0.5);
instantiatedProjectile.rigidbody.useGravity = false;
instantiatedProjectile.rigidbody.constantForce.force = (Vector3.forward*10);
yield WaitForSeconds(reloadTime);
missilefire = false;
}
//Physics.IgnoreCollision(instantiatedProjectile.collider, transform.root.collider);
//not really needed
lastShot = Time.time;
ammoCount--;
}
}
sure thing. i'll put it below the shortened version.
so what do you think is wrong with it? it's not putting the script on the cloned object, so it must be something with my declarations.
why can't the instantiated prefab already have those scripts attached to begin with? i think you may be making things more complicated than they need to be.
Answer by sman_47o · Aug 14, 2012 at 09:17 PM
heres my answer that will definitely streamline your code and more than likely fix it. you need to attach all those scripts to the object instead of adding them on runtime. add the projectile gameObject to your project files and instead of having to destroy it you will just reference it every time you spawn a missile.
also you could get rid of having a whole other script to move the missile why don't you write one inside of the script that you posted and simply call a function that sets the rigidBodies also to stop the downward movement caused by gravity use rigidbody.UseGravity(false);
the reason I haven't put it in project files is because there's 8 individual missile GameObjects and I wanted to put this script on them. but in that case, why don't I just forget about instantiating and just manipulate the GameObjects themselves? facepalm
you could do that haha but instantiate makes a clone so you can have several instances of the same object in the game XD
so what are you trying to do? only allow for eight shots? and I'm right in thinking that this script is inside of the missile object right?
I originally used $$anonymous$$oveTowards, but the missile wouldn't actually hit the target, it'd get close and then repel away. I also just now took out transform.LookAt and threw a SmoothLookAt script on the missile GO (game object) for more functionality.
Um, yeah, it's just a short educational (but action-packed!) game I'm making, it doesn't have to have over 8 shots and a reload process or anything. it'd be nice though, perhaps I can work on that later.
Currently, my objective is to make it so that each time space is pressed, it fires a missile from a different location on the pylon(the thing holding the missiles). I want to get the tracking and explosion down first though.
well if you just instantiate the object you could hit two birds with one stone, make a smoother running game and it will be easier to implement the reload system later.
Answer by herp johnson · Aug 14, 2012 at 09:45 PM
Ok here's MoveObject. for more info on it go Here
enum MoveType {Time, Speed} static var use : MoveObject;
function Awake () {
if (use) {
Debug.LogWarning("Only one instance of the MoveObject script in a scene is allowed");
return;
}
use = this;
}
function Translation (thisTransform : Transform, endPos : Vector3, value : float, moveType : MoveType) {
yield Translation (thisTransform, thisTransform.position, thisTransform.position + endPos, value, moveType);
}
function Translation (thisTransform : Transform, startPos : Vector3, endPos : Vector3, value : float, moveType : MoveType) {
var rate = (moveType == MoveType.Time)? 1.0/value : 1.0/Vector3.Distance(startPos, endPos) * value;
var t = 0.0;
while (t < 1.0) {
t += Time.deltaTime * rate;
thisTransform.position = Vector3.Lerp(startPos, endPos, t);
yield;
}
}
function Rotation (thisTransform : Transform, degrees : Vector3, time : float) {
var startRotation = thisTransform.rotation;
var endRotation = thisTransform.rotation * Quaternion.Euler(degrees);
var rate = 1.0/time;
var t = 0.0;
while (t < 1.0) {
t += Time.deltaTime * rate;
thisTransform.rotation = Quaternion.Slerp(startRotation, endRotation, t);
yield;
}
}
Hey im sorry I never got back to you the other day I lost power and I'm currently s$$anonymous$$ling Internet from my neighbor XD I'll try and get something written from my iPod. But if i can't I can at least point you in the right direction. Use the lookAt function to get your missile (which if i were you id make kinematic so that gravity wont effect it) to look at your target and then set its velocity to transform.forward * speed (speed would be a float that you can fiddle around with to make it do what you want) that's all you really need to move your object. (and if velocity gives you crap, it loves to be difficult, you can also try using translate, add relative force, or position but use velocity if at all possible). Good luck to you and I hope I can some power back soon after they deal with this tree) the world needs educational games with explosives I would have love phonics if I had gotten the reward of demolishing something.
actually I got rid of the instantiation altogether. it was too much trouble. unity reccomends not using velocity so i used Vector3.$$anonymous$$oveTowards, which is more dynamic and doesn't screw you over when using colliders. now everything works! :D
Your answer
![](https://koobas.hobune.stream/wayback/20220613075658im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
trouble adding script to clone object 1 Answer
Code only affects first clone? 2 Answers
Cloning Problem Script 0 Answers
How do I assign a script to a clone through another script? 1 Answer
Duplicate Object, by dragging? (SporeCreatureCreator Like) 0 Answers