Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by yoda12 · Feb 11, 2015 at 11:48 PM · javascriptinstantiatearrayconfusedstatic-vars

[--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: DOES NOT WORK AND IT IS USING CTRL.JS

WORKS AND USES BFG.js: WORKS AND USES bfg.js

***HERE IS A VIDEO***

ddd.png (452.9 kB)
ddr.png (454.4 kB)
Comment
Add comment · Show 23
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image incorrect · Feb 11, 2015 at 11:52 PM 3
Share

That's not 'the problem' with static variables, that's what they are actually used for.

avatar image Bonfire-Boy · Feb 12, 2015 at 12:16 AM 1
Share

With respect to the Update... which variable is it that you think should be static?

avatar image incorrect · Feb 12, 2015 at 12:47 AM 1
Share

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.

avatar image incorrect · Feb 12, 2015 at 12:49 AM 1
Share

Btw, it's nice to see you putting so much effort in solving your problem. The community can be proud of you! :)

avatar image Bonfire-Boy · Feb 12, 2015 at 01:24 AM 1
Share

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.

Show more comments

3 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image yoda12 · Feb 12, 2015 at 02:25 AM 0
Share

Thanks again! I can now move to other parts of my game!

avatar image incorrect · Feb 12, 2015 at 02:40 AM 1
Share

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!

avatar image
3

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.

Comment
Add comment · Show 6 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image yoda12 · Feb 11, 2015 at 11:53 PM 0
Share

How would I use an instanced variable in this case then? And, thank you for you're direct answer!

avatar image Bonfire-Boy · Feb 11, 2015 at 11:59 PM 1
Share

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.

avatar image Kiwasi · Feb 12, 2015 at 01:35 AM 1
Share

Just delete the key word static wherever it appear in your scripts. You appear to be a beginner, you should not be touching static.

avatar image yoda12 · Feb 12, 2015 at 02:19 AM 1
Share

Actually, I am still learning, but I was not sure if statics were like they are! Thanks for all the help!

avatar image incorrect · Feb 12, 2015 at 02:31 AM 1
Share

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.

Show more comments
avatar image
0

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);
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Array variable assigned to instantiated object changes in all instantiated objects? 1 Answer

How can I instantiate a GameObject directly into an array? 3 Answers

Instantiate object in for loop with array 2 Answers

Instantiating from an object that's in an array 1 Answer

Instantiate into array : Out of range? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges