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
0
Question by koca2000 · Dec 02, 2014 at 03:36 PM · variable

Problem with variables

I have two types of boats in my 2D game. I'm cloning boats and sending to another side of screen. First type works fine, but second no. When i use breakpoints to find problem, i clone first boat of sekond type fine, but second was cloned to same x coordinates. I check array with childs transforms and everythink was null.

First type script:

 using UnityEngine;
 using System.Collections;
 
 public class lod_normal_vpravo : MonoBehaviour
 {
 
         public static bool spawnship;
         static Transform[] childs;
         
         // Use this for initialization
         void Start ()
         {
             childs = new Transform[40];
         }
     
         // Update is called once per frame
         void Update ()
         {
                 if (spawnship) {
                         SpawnChild ();
                 }
 
                 for (int i = 0; i<childs.Length; i++) {
                         if (childs [i] != null) {
                                 childs [i].Translate (Vector3.left * Time.deltaTime, Camera.main.transform);
                                 if (childs [i].position.x < 0 - childs [i].renderer.bounds.size.x) {
                                         Destroy (childs [i].gameObject);
                                         childs [i] = null;
                                 }
                         }
                 }
                 //transform.Translate (Vector3.left * Time.deltaTime, Camera.main.transform);
         }
 
         void SpawnChild ()
         {
                 bool volno = true;
                 int i = 0;
                 while (volno) {
                         if (childs [i] == null) {
                                 volno = false;
                         } else {
                                 if (i == 39) {
                                         return;
                                 }
                                 i++;
                         }
                 }
 
                 childs [i] = Instantiate (transform) as Transform;
                 float position = Random.Range (6.5f, 9.8f);
                 childs [i].localPosition = new Vector3(childs [i].position.x, position, childs [i].position.z);
                 spawnship = false;
         }
 }
 

Second type script:

 using UnityEngine;
 using System.Collections;
 
 public class lod_normal_vlevo : MonoBehaviour {
 
     public static bool spawnship;
     Transform[] childs;
 
     // Use this for initialization
     void Start () {
         childs = new Transform[40];
     }
     
     // Update is called once per frame
     void Update () {
         if (spawnship) {
             SpawnChild ();
         }
         
         for (int i = 0; i<childs.Length; i++) {
             if (childs [i] != null) {
                 childs [i].Translate (Vector3.right * Time.deltaTime, Camera.main.transform);
                 if (childs [i].position.x > Screen.width) {
                     Destroy (childs [i].gameObject);
                     childs [i] = null;
                 }
             }
         }
     }
 
     void SpawnChild ()
     {
         bool volno = true;
         int i = 0;
         while (volno) {
             if (childs [i] == null) {
                 volno = false;
             } else {
                 if (i == 39) {
                     return;
                 }
                 i++;
             }
         }
         
         childs [i] = Instantiate (transform) as Transform;
         float position = Random.Range (6.5f, 9.8f);
         childs [i].localPosition = new Vector3(childs [i].position.x, position, childs [i].position.z);
         spawnship = false;
     }
 }
 
Comment
Add comment · Show 2
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 NoseKills · Dec 02, 2014 at 05:04 PM 0
Share

First type works fine, but second no.

Do you get an error or is it just that the coordinates are wrong? Where do you set spawnship of either script back to true?

was cloned to same x coordinates

You mean the same spot where the first got destroyed? Or correct side of screen but same y coordinate as first?

I check array with childs transforms and everythink was null.

At what point do you check it? You call destroy on the childs array at the end of the loop so I'd expect it to be full of null

avatar image koca2000 · Dec 02, 2014 at 05:19 PM 0
Share

Spawnship true i set in $$anonymous$$ainCamera script(i use it like event listener and timer).Screenshot of problem I check it in SpawnChild void. First boat normally go from one side to another and second is spawned on same x coordinates but different y coordinates. And destroy don´t work too.

screenshot.png (443.5 kB)

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by SkaredCreations · Dec 02, 2014 at 07:08 PM

Of course, since you're setting the same X for localPosition in SpawnChild. So since you're not passing a location to Instantiate so it's spawning to the default position (Vector3.zero), then you're not changing X but only Y with the Random.Range

PS: also consider that using "localPosition" is not different from world position, because you're not parenting the new instantiated object to any other so those are really world coordinates and not local.

Comment
Add comment · Show 5 · 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 koca2000 · Dec 02, 2014 at 07:22 PM 0
Share

So what I have to do to get it working? If it help i create clone of clone i don´t know why.

avatar image SkaredCreations · Dec 02, 2014 at 10:39 PM 0
Share

Honestly I'd divide your logic into 2 scripts: one spawns the ships (and maintains a pool array by deactivating/activating the objects in the list, so that it'll not create/destroy objects every time but instantiate only once for better performance) and one handles the movement (and deactivate itself once it is out of screen). The spawner script would have a public variable where you'll drag the ship prefab.

avatar image koca2000 · Dec 03, 2014 at 10:22 AM 0
Share

But how can i use two scripts for one object?

avatar image gjf · Dec 03, 2014 at 10:26 AM 0
Share

you can add as many scripts as you like to any object; try to make each perform a single task.

avatar image koca2000 · Dec 03, 2014 at 11:30 AM 0
Share

you can add as many scripts as you like to any object; try to make each perform a single task.

So why when I start game, Unity doesn´t respond.

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Display additional text before the variable that the user is editing in a GUI Text Field 1 Answer

Setting static variables in Editor script 3 Answers

How to set a public variable back to it's original value? 1 Answer

The variable has not been assigned 2 Answers

How to batch-change access to variables? 0 Answers


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