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 WalaKauSaLoloKo · Aug 01, 2012 at 01:05 PM ·

Whats wrong with this code (C# script)???

Whats wrong with this script??? i put this script in all of my obects in my List(myWalls).. theres no error in this code... but my objects are not moving... anyone knows why??? thanks in advance ... :))

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 
 public class move : MonoBehaviour
 {
     public float Speed = 5;
     public GameObject randomWall;
     public List<GameObject> myWalls = new List<GameObject>();
     void Start()
     {
         RandomWALLS();
     }
     
     void Update ()
     {
         movingWalls();
     }
     
     void movingWalls()
     {
         float amtToMove = Speed * Time.deltaTime;
         randomWall.transform.Translate(Vector3.forward * amtToMove);
         if (randomWall.transform.position.y < -8)
         {
             GameObject.Destroy(randomWall);
         }
     }
     
     void RandomWALLS()
     {
         int WallIndex = Random.Range (0, myWalls.Count);
         randomWall = myWalls[WallIndex];
     }
 }
 
Comment
Add comment · Show 17
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 whydoidoit · Aug 01, 2012 at 01:05 PM 0
Share

Is Speed set to 0 in the inspector?

avatar image WalaKauSaLoloKo · Aug 01, 2012 at 01:20 PM 0
Share

speed is set to 5 in the inspector.... thanks for the reply :)

avatar image whydoidoit · Aug 01, 2012 at 01:23 PM 0
Share

Damn - it looks right otherwise :)

Worth a Debug.Log on amtTo$$anonymous$$ove and randomWall.transform.position I think...

avatar image WalaKauSaLoloKo · Aug 01, 2012 at 01:35 PM 0
Share

yeah theres no error with my codes... thats why im having trouble with this.... sir what do you mean by worth a debug.Log???

avatar image CHPedersen · Aug 01, 2012 at 01:49 PM 0
Share

I edited your post to format the code part. What he means is, insert calls to Debug.Log in key places (like in the Update) to make sure it's actually getting called, and then afterwards, in movingWalls, to print the value of Vector3.forward * amtTo$$anonymous$$ove to the console, to make sure you're not passing something unexpected to transform.Translate.

Show more comments

3 Replies

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

Answer by Languard · Aug 01, 2012 at 02:45 PM

Several things.

  1. Don't use GameObject, use Transform. Just changing the list to Transforms fixes the problem. (actually, ignore this. I'm not sure what the original problem was, but when I changed my code from Transform to GameObject, that screwed up the references in the editor. I had 'type mismatch' listed instead of my cubes. Re-adding the cubes to the list fixed it, and it works using GameObject. So you might have to re-add all the wall pieces if you've been switching the types around)

  2. If you change to Transform, then randomWall needs to be a Tranform of course.

  3. Only one wall will ever get moved at the start. Not sure if that is your intent or not.

  4. The moving wall will never get destroyed. You're checking against Y, but the wall is moving in Z.

  5. Assuming you change the destruction check to look at Z, after the wall is destroyed your code will still be trying to move it. Also, you don't remove the wall from the list so the destroyed wall could get chosen again if you called RandomWALLS() a second time.

Here is the changes I made, and it works. (final time. No. Really. This is it. And it works!)

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 
 
 public class move: MonoBehaviour
 {
     public float Speed = 5;
     public GameObject randomWall;
     public List<GameObject> myWalls;
     int curIndex;
 
 
     void Start()
     {
         RandomWALLS();
     }
 
     void Update()
     {
         movingWalls();
         //Invoke(&quot;movingWalls&quot;,5);
     }
 
     void movingWalls()
      {
 
          if (randomWall == null) return;
 
         float amtToMove = Speed * Time.deltaTime;
 
         randomWall.transform.Translate(Vector3.forward * amtToMove);
 
         if (randomWall.transform.position.z > 8)
         {
             GameObject.Destroy(randomWall);
             randomWall = null;
             myWalls.RemoveAt(curIndex);
             if (myWalls.Count > 0)
             {
                 RandomWALLS();
             }
         }
      }
     void RandomWALLS()
     {
         curIndex = Random.Range(0, myWalls.Count);
         randomWall = myWalls[curIndex];
     }
 
 }
Comment
Add comment · Show 15 · 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 whydoidoit · Aug 01, 2012 at 02:57 PM 0
Share

He didn't have a List on its own - unfortunately when the code was formatted by someone else the nightmare that is Unity Answers code formatting post UDN had stripped the generic parameters. See my question here

avatar image Languard · Aug 01, 2012 at 03:01 PM 0
Share

Well with that piece of information, it seems the bug is in using a GameObject ins$$anonymous$$d of a Transform. Just tested it with GameObjects and it won't move. But use Transforms and the code works.

avatar image whydoidoit · Aug 01, 2012 at 03:08 PM 0
Share

I presume the original code was a List<GameObject> but also in his original code he is doing randomWall.transform - can't see why that wouldn't work. Am I missing something?

avatar image Languard · Aug 01, 2012 at 03:12 PM 1
Share

I'm not sure. Running some tests right now but I almost never use GameObjects, I use Transforms in all my code. Also there is a critical bug in that after the wall is destroyed, it will still be trying to move it. And the destroyed wall isn't removed from the list so that's a problem as well.

avatar image Bunny83 · Aug 04, 2012 at 12:42 PM 1
Share

In RandomWALLS you create another local variable curIndex, so the member variable stays untouched. I will remove the "int"...

Show more comments
avatar image
0

Answer by anonymousUser · Aug 01, 2012 at 06:00 PM

Are the walls scaled?

Have you tried

 randomWall.transform.Translate(Vector3.forward * amtToMove, Space.World);
Comment
Add comment · Show 1 · 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 WalaKauSaLoloKo · Aug 04, 2012 at 01:16 PM 0
Share

thanks for the reply... this already been fixed thanks! :D

avatar image
0

Answer by Akill · Aug 04, 2012 at 11:01 AM

Is your wall a rigidbody/Physics object? If so, try settings its isKinematic flag to true.

Comment
Add comment · Show 7 · 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 Bunny83 · Aug 04, 2012 at 12:40 PM 0
Share

I don't see that it's fixed since no answer is marked as soultion. Also the answer of Languard has a typo / wrong line.

avatar image WalaKauSaLoloKo · Aug 04, 2012 at 01:01 PM 0
Share

@Bunny83 his code is working.... but there is a problem when shuffling 5 objects... there is a chance that a randomWall will select a missing object(destroyed)... sorry for duplicating my question sir..

avatar image Bunny83 · Aug 04, 2012 at 01:11 PM 0
Share

No, the problem is that a destroyed gameobject should be removed from the list. Since he never set curIndex, this line myWalls.RemoveAt(curIndex); will always remove the first gameobject and not the one you're currently using.

I've fixed his answer ;)

avatar image WalaKauSaLoloKo · Aug 04, 2012 at 01:18 PM 0
Share

yeah you fixed it sir! :)) thanks a lot! :D

avatar image WalaKauSaLoloKo · Aug 04, 2012 at 01:21 PM 0
Share

@Bunny83 it starts with the fastest down to the slowest... why o why?

Show more comments

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

12 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Efficient GUI resizing 0 Answers

How do i log a game objects position to text file 0 Answers

OnMouseEnter, change color. 1 Answer

Global name space error c# 0 Answers

MonoDevelop shows splash screen then never launches 4 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