- Home /
[--FIXED--][UPDATE#5][VIDEO]Instantiating problem(OLD TITLE:How do I change the static variable of one object instead of all instances
[UPDATE]I AM GOING TO CLEAN THIS UP
VIDEO AT BOTTOM OF PAGE
I want to thank you all for you're time and help, and especially incorrect for the solution!
So, I have turrets, that each have the same script. They all have a "fire" variable which is call atk. I have found a solution, but It needs some work: I don't want to go into how this all worked out, but basicly I needed to have a script control another script, and here it is:
The title is a bit too simple, so sorry. My problem is this:
I have an object called Turret. Turret has a child named spawnPoint. Turret has a script called spawn and that script has a timer to only spawn a laser prefab every 1/2 second or so, and also looks at target: transform.LookAt(atar); The laser prefab has no gravity and has rigidbody Some of the Code to instantiate:
(I didn't post the timer by the way) So this works, and it always follows the target, and fires lasers directly at it, and the lasers are always facing the correct direction: The short side is pointed at it :
[Target]
^<-- Direction the laser is going
|
|<- laser
But here is my problem:
In another script, I tell Turret to transform.LookAt(atar.trnasform), an then use the sane instantiate code to spawn a laser: The only difference is that it finds the Turrets child object:
And what ends up happening is that the lasers face the wrong direction, and transform.forward brings them the worng way! I have tried Vector3.left and -transform.right, but that just makes them only get projected forward, not and angle like the other script does. It also rotates the wrong way. I have tried to fix this and ont know whats makeing it do this. Please help!
[Target]
Direction its going
٧
__ >
^Laser
Alright, here is the Script that had the static variables: (Its a bit messy) (bfg.js)It works perfectly by it self (not including the problem with the static variables)HERE IS bfg.js
#pragma strict
var curtar = "";
var fore = 1000;
var damping = 6.0;
var smooth = true;
var player : GameObject;
var laserb:Transform;
var st;
var dbp = true;
var atk = false; //THIS USED TO BE STATIC
static var atar : GameObject;
function Update(){
//atar = GameObject.FindWithTag("fd");
if (atk == true) {
atar = GameObject.FindWithTag("fd");
transform.LookAt(atar.transform);
var seconds = Time.time;
//var next = (seconds % 2);
dbp = true;
fire(seconds);
}
}
var df = transform.rotation;
var lastFired = 0 ;
var target : Transform;
function fire(seconds) {
if (Time.time > lastFired + 1.0){
atar = GameObject.FindWithTag("fd");
//var plo = atar.transform;
var laser = Instantiate(laserb, transform.Find("spawnPoint").transform.position,
Quaternion.identity);
//laser.transform.LookAt(atar.transform);
laser.rigidbody.AddForce(transform.forward * fore);
laser.transform.LookAt(atar.transform);
//laser.rigidbody.AddForce(Vector3.Dot * 5 * 5);
var laser1 = Instantiate(laserb, transform.Find("spawnPoint1").transform.position,
Quaternion.identity);
laser1.rigidbody.AddForce(transform.forward * fore);
laser1.transform.LookAt(atar.transform);
lastFired = Time.time;
yield WaitForSeconds(0.25);
laser = Instantiate(laserb, transform.Find("spawnPoint").transform.position,
Quaternion.identity);
laser.rigidbody.AddForce(transform.forward * fore);
laser.transform.LookAt(atar.transform);
laser1 = Instantiate(laserb, transform.Find("spawnPoint1").transform.position,
Quaternion.identity);
laser1.rigidbody.AddForce(transform.forward * fore);
laser1.transform.LookAt(atar.transform);
yield WaitForSeconds(0.25);
laser = Instantiate(laserb, transform.Find("spawnPoint").transform.position,
Quaternion.identity);
laser.rigidbody.AddForce(transform.forward * fore);
laser.transform.LookAt(atar.transform);
laser1 = Instantiate(laserb, transform.Find("spawnPoint1").transform.position,
Quaternion.identity);
laser1.rigidbody.AddForce(transform.forward * fore);
laser1.transform.LookAt(atar.transform);
yield WaitForSeconds(0.25);
laser = Instantiate(laserb, transform.Find("spawnPoint").transform.position,
Quaternion.identity);
laser.rigidbody.AddForce(transform.forward * fore);
laser.transform.LookAt(atar.transform);
atar = GameObject.FindWithTag("fd");
laser1 = Instantiate(laserb, transform.Find("spawnPoint1").transform.position,
Quaternion.identity);
laser1.rigidbody.AddForce(transform.forward * fore);
laser1.transform.LookAt(atar.transform);
st = seconds;
dbp = false;
}else {
}
}
PROBLEM-- OKAY HERE IS THE FILE THAT HAS THE PROBLEM: So turrets[] is an array of turret objects, and then d is a turret. This file is ctrl.js
#pragma strict
var targ = "";
static var attr = "";
var sel = false;
var attack = false;
var turrets : GameObject[];
var targeter = false;
var target : GameObject;
var d : GameObject;
var laserb : GameObject;
var fore = 1000;
function Start () {
}
var atar : GameObject;
function Update(){
if (sel == true) {
if (targeter == true){
//turrets[i].atar =
//turrets[1].GetComponent(bfg).atk = true;
//var def : bfg;
//def = turrets[1].gameObject.GetComponent(bfg);
//bfg.atk =
fire();
}
}
}
var lastFired = 0;
function fire() {
if (Time.time > lastFired + 1.0){
atar = GameObject.FindWithTag("fd");
d.transform.LookAt(atar.transform);
var fg = turrets[0];
var laser1 = Instantiate(laserb, d.transform.Find("spawnPoint").transform.position,
Quaternion.identity);
laser1.rigidbody.AddForce(transform.forward * fore);
laser1.transform.LookAt(atar.transform);
lastFired = Time.time;
yield WaitForSeconds(0.25);
}
}
DOES NOT WORK AND USES CTRL.JS:
WORKS AND USES BFG.js:
That's not 'the problem' with static variables, that's what they are actually used for.
With respect to the Update... which variable is it that you think should be static?
As long as no-one can find a mistake in your code, let's try to check if those variables have correct values.
var laser1 = Instantiate(laserb, transform.Find("spawnPoint1").transform.position,
Quaternion.identity);
laser1.rigidbody.AddForce(transform.forward * fore);
laser1.transform.LookAt(atar.transform);
Debug.DrawLine (transform.Find("spawnPoint1").transform.position, atar.transform.position, Color.red, 1, false);
Debug.Log ("The target is " + atar.name);
lastFired = Time.time;
$$anonymous$$ake shooting like this adding those two lines of debug, they will show where you are shooting and what's the current target's name.
Btw, it's nice to see you putting so much effort in solving your problem. The community can be proud of you! :)
The fact that atar is static, reset multiple times in the update() function, but also used following yields without setting it locally again first, is kind of suspicious. If multiple turrets exist, they would be influencing each other's behaviour. So another useful simplification would be starting with just the one turret.
Answer by incorrect · Feb 12, 2015 at 02:24 AM
Maybe it should be like this?
var laser1 = Instantiate(laserb, fg.transform.Find("spawnPoint").transform.position,
Quaternion.identity);
laser1.rigidbody.AddForce(fg.transform.forward * fore);
laser1.transform.LookAt(atar.transform);
Because on your picture lasers are faced right direction, but fly in a wrong one.
Remember that it is crucial to which gameobject your script is attached to. It affects which transform, gameobject and all those MonoBehaviour's properties will be returned when you try to access them with keywords 'trasform', 'gameobject', etc.
So if your BFG.js script instance is attached to the ship and not to the turret, line laser1.rigidbody.AddForce(transform.forward * fore);
will make a laser beam fly along the z-axis of the ship, because transfor.forward returns forward direction of the object the script is attached to. Instead to get the forward direction of a turret you should get a reference to the turret's transform and use it's turretTransform.forward.
yoda12, I'm sure you'll make a nice one if you keep working so hard on what you want to do! But consider learning a bit about program$$anonymous$$g and trying some tutorials, especially those official unity ones HERE, because as your game grows more and more large and complex and if it has many little mistakes in code or architecture, it becomes difficult to continue development, and at some point it simply stops, because you are unable to resolve so many problems and even community can not help you. Every experienced coder faced that problem, so try to code the right way as much as possible. Good luck to you!
Answer by Kiwasi · Feb 11, 2015 at 11:50 PM
It cannot be done. Static variables, by their nature, are shared.
Use an instance variable instead.
How would I use an instanced variable in this case then? And, thank you for you're direct answer!
Can you explain why you want to use a static variable? It just seems like the wrong thing here. Can you explain what is it about a non-static that would be problematic in this case? None of this is clear from the question.
Just delete the key word static wherever it appear in your scripts. You appear to be a beginner, you should not be touching static.
Actually, I am still learning, but I was not sure if statics were like they are! Thanks for all the help!
yoda12, if you want all of your turrets shoot the same target, it makes sense to use a static variable for target. But it may cause some problems if you yield shooting and the target changes, but you do not update turret's firing, so when yielded fire function comes to life, it does not get information that target has changed, so it can get wrong parameters for instantiating, just as Bonfire Boy said.
On the other hand, you can make funny things like burst fire, making turrets fire several times in a target before it can stop or change target. In that case you need to store firing information (direction to the target) in the function itself, so it will be preserved from changing.
Answer by yoda12 · Feb 12, 2015 at 02:24 AM
SOLUTION: Thanks, incorrect! Here is the final fix from incorrect:
var laser1 = Instantiate(laserb, fg.transform.Find("spawnPoint").transform.position,
Quaternion.identity);
laser1.rigidbody.AddForce(fg.transform.forward * fore);
laser1.transform.LookAt(atar.transform);
Your answer
![](https://koobas.hobune.stream/wayback/20220613183057im_/https://answers.unity.com/themes/thub/images/avi.jpg)