- Home /
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];
}
}
speed is set to 5 in the inspector.... thanks for the reply :)
Damn - it looks right otherwise :)
Worth a Debug.Log on amtTo$$anonymous$$ove and randomWall.transform.position I think...
yeah theres no error with my codes... thats why im having trouble with this.... sir what do you mean by worth a debug.Log???
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.
Answer by Languard · Aug 01, 2012 at 02:45 PM
Several things.
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)
If you change to Transform, then randomWall needs to be a Tranform of course.
Only one wall will ever get moved at the start. Not sure if that is your intent or not.
The moving wall will never get destroyed. You're checking against Y, but the wall is moving in Z.
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("movingWalls",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];
}
}
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
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.
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?
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.
In RandomWALLS you create another local variable curIndex, so the member variable stays untouched. I will remove the "int"...
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);
thanks for the reply... this already been fixed thanks! :D
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.
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.
@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..
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 ;)
@Bunny83 it starts with the fastest down to the slowest... why o why?
Your answer
Follow this Question
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