Search Unity

I am very new to this unity tool. need some help

Discussion in 'Editor & General Support' started by suresh, Mar 29, 2011.

  1. Jesse Anders

    Jesse Anders

    Joined:
    Apr 5, 2008
    Posts:
    2,857
    Hm, I can only guess as to what might be wrong. Make sure the range of coordinates you're using for instantiating the food makes sense, and that the food isn't instantiating off of or under the board somewhere. You might also try pausing the game, selecting the food in the hierarchy, and then moving the cursor over the 'scene' view and pressing 'F'. This will focus the camera on the food and show you where it is in the scene.
     
  2. suresh

    suresh

    Joined:
    Mar 29, 2011
    Posts:
    45
    Yes, thanks @Jesse Anders, i set it and now its visible on the floor, but i am unable to destroy that Food(Clone) with snake.
    , now this is the problem
     
  3. Jesse Anders

    Jesse Anders

    Joined:
    Apr 5, 2008
    Posts:
    2,857
    It's probably because the new food item is not named 'Food', but rather 'Food (Clone)', or something like that.

    To fix the problem, try using tags instead, as previously suggested.
     
  4. suresh

    suresh

    Joined:
    Mar 29, 2011
    Posts:
    45
    it really worked and now the problem, and now snake problem: need to create a new snake when the old snake dead.
     
  5. Jesse Anders

    Jesse Anders

    Joined:
    Apr 5, 2008
    Posts:
    2,857
    What problem are you having? Your original code looks ok, other than the fact that you're instantiating the new snake right where the old one was.
     
  6. suresh

    suresh

    Joined:
    Mar 29, 2011
    Posts:
    45
    When i try to play the game with the bellow code its giving an error

    isn't it the right way

    Code (csharp):
    1. if(theCollision.gameObject.name == "Wall")
    2.   {
    3.   Debug.Log("Hit the wall");
    4.   Destroy(gameObject);
    5.  var position = Vector3(Random.Range(-3,10), 0.284 ,Random.Range(-10, 10));
    6.  Instantiate(food,  position, Quaternion.identity);
    7.   }
    8.   }
    9.  

    When i try to play with this code, it created the Snake(Clone), but it is not behaving like the first snake.

    Code (csharp):
    1. if(theCollision.gameObject.name == "Wall")
    2.   {
    3.   Debug.Log("Hit the wall");
    4.   Destroy(gameObject);
    5.  Instantiate(snake, transform.position, transform.rotation);
    6.   }
    7.   }
    8.  
     
  7. Jesse Anders

    Jesse Anders

    Joined:
    Apr 5, 2008
    Posts:
    2,857
    I'm not familiar with UnityScript's scoping rules, so I'll have to leave that one to someone else.
     
  8. suresh

    suresh

    Joined:
    Mar 29, 2011
    Posts:
    45
    ok then leave that for this movement. The other problem is that when it eat food the tail should grow, can any one tell me how it would be...
     
    Last edited: Apr 6, 2011
  9. suresh

    suresh

    Joined:
    Mar 29, 2011
    Posts:
    45
    When i try to play the game with the bellow code, the Snake(Clone) is created after the first snake got dead, but the Snake(Clone) is rotating rotating and moving abnormally with out doing anything
    Code (csharp):
    1.  
    2. var snake : Transform;
    3.  
    4. function OnCollisionEnter(theCollision : Collision){
    5.  if(theCollision.gameObject.name == "Wall"){
    6.   Debug.Log("Hit the wall");
    7.   Destroy(gameObject);
    8.   var position = Vector3(Random.Range(-3,10), 0.284 ,Random.Range(-10, 10));
    9.   Instantiate(snake, position, Quaternion.identity);
    10. }
    11. }
    12.  
    can any one help me out in this code, how to stop that abnormal behavior of the Snake(Clone)
     
  10. Jesse Anders

    Jesse Anders

    Joined:
    Apr 5, 2008
    Posts:
    2,857
    I don't think there's enough information there to determine what's causing that behavior. You may just need to do some debugging, e.g. add Debug.Log() statements and use other diagnostic methods to determine what's causing the problem.
     
  11. suresh

    suresh

    Joined:
    Mar 29, 2011
    Posts:
    45
    I have solved the old problems, now got new problem. Every thing is working fine except new snake, when the first snake dead the new snake is creating but its not performing like the old snake.I tried to figure out the problem, i got to know that the problem is when ever the new snake got created the Snake script(CHECK BOX) in the
    is getting unchecked, if I tick that check box the snake is working fine otherwise its not working. One more problem is that i SmooothFollow camera is not following the new snake.

    This is the code that i am working on ....
    Code (csharp):
    1.  
    2. var speed = 5.0;
    3. var food : Transform;
    4. var snake : Transform;
    5.  
    6. function Update ()
    7. {
    8. var x = Input.GetAxis("Horizontal") * Time.deltaTime * speed;
    9. var z = Input.GetAxis("Vertical") * Time.deltaTime * speed;
    10. transform.Translate(x, 0, z);
    11. }
    12. function OnCollisionEnter(theCollision : Collision)
    13. {
    14.  
    15.  if(theCollision.gameObject.tag == "Food")
    16.  {
    17.   Debug.Log("Hit the Food");
    18.   Destroy(theCollision.gameObject);
    19.   var position = Vector3(Random.Range(-3,10), 0.284 ,Random.Range(-10, 10));
    20.   Instantiate(food, position, Quaternion.identity);
    21.   }
    22.   else
    23.   if(theCollision.gameObject.tag == "Wall"){
    24.   Debug.Log("Snake hit the wall");
    25.   Destroy(gameObject);
    26.   Debug.Log("Snake dead");
    27.   Instantiate(snake, Vector3(Random.Range(-3,10), 0.375 ,Random.Range(-9, 9)), Quaternion.identity);
    28.   Debug.Log("Camera for the Snake");
    29.   gameObject.Find("Main Camera").transform.position = Vector3(0,7,-15);
    30.   Debug.Log("Got new Camera for the Snake");
    31.  
    32. }
    33. }
    34.  

    Can any one help me out in solving this problem..........
     
    Last edited: Apr 7, 2011
  12. suresh

    suresh

    Joined:
    Mar 29, 2011
    Posts:
    45
    my god i have solved the problem of new snake, now only one problem is there with the new snake, that is the camera(Soomthfollow) is not following the new snake.
     
    Last edited: Apr 7, 2011
  13. Jesse Anders

    Jesse Anders

    Joined:
    Apr 5, 2008
    Posts:
    2,857
    My guess would be that the camera was storing a reference to the original snake, and that you're not updating that reference when the new snake is instantiated.

    If that is in fact the problem, you'll need to assign a reference to the new snake game object to the appropriate variable in the camera script when the new snake is created.
     
  14. suresh

    suresh

    Joined:
    Mar 29, 2011
    Posts:
    45
    i tried to do so, but its giving problems...
     
  15. Jesse Anders

    Jesse Anders

    Joined:
    Apr 5, 2008
    Posts:
    2,857
    There is no information in your statement, therefore it's impossible to provide any help.
     
  16. suresh

    suresh

    Joined:
    Mar 29, 2011
    Posts:
    45
    I did like the way in my script for the, wit that when i try to play the game, i am getting null-pointer exception.
    I don't know why it is like the way.......camera(smoothfollow) is not following the new snake.
    Code (csharp):
    1.  
    2.  if(theCollision.gameObject.tag == "Wall")
    3. {
    4.   Debug.Log("Snake hit the wall");
    5.   Debug.Log("Snake dead");
    6.   Instantiate(snake, Vector3(Random.Range(-3,10), 0.375 ,Random.Range(-9, 9)), Quaternion.identity);
    7.   Debug.Log("Camera for the Snake");
    8.   gameObject.Find("SmoothFollow").transform.position = Vector3(-0.5147346,20,-25.82903);
    9.   Debug.Log("Got new Camera for the Snake");
    10.   Destroy(gameObject);
    11. }
    12. }
    13.  


    More over i am now working on tail, when the snake eat food the tail should grow, can i have some idea on that part.
     
  17. Jesse Anders

    Jesse Anders

    Joined:
    Apr 5, 2008
    Posts:
    2,857
    One problem at a time :)

    Regarding the camera, you most likely need to assign a reference to the new snake object to a variable in the SmoothFollow script. Instantiate() returns a reference to the newly created object, so you can grab the reference from there. You can then assign the reference to the appropriate variable of the SmoothFollow script in much the same way that you're setting the camera's position.
     
  18. suresh

    suresh

    Joined:
    Mar 29, 2011
    Posts:
    45
    The bellow is the script for smoothfollow camera, i don't know where to put, can you please tell me where exactly i can insert what you are telling me to do.

    Code (csharp):
    1.  
    2. var target : Transform;
    3. var distance = 10.0;
    4. var height = 5.0;
    5. var heightDamping = 2.0;
    6. var rotationDamping = 3.0;
    7.  
    8. @script AddComponentMenu("Camera-Control/Smooth Follow")
    9.  
    10.  
    11. function LateUpdate () {
    12.     // Early out if we don't have a target
    13.     if (!target)
    14.         return;
    15.    
    16.     // Calculate the current rotation angles
    17.     wantedRotationAngle = target.eulerAngles.y;
    18.     wantedHeight = target.position.y + height;
    19.        
    20.     currentRotationAngle = transform.eulerAngles.y;
    21.     currentHeight = transform.position.y;
    22.    
    23.     // Damp the rotation around the y-axis
    24.     currentRotationAngle = Mathf.LerpAngle (currentRotationAngle, wantedRotationAngle, rotationDamping * Time.deltaTime);
    25.  
    26.     // Damp the height
    27.     currentHeight = Mathf.Lerp (currentHeight, wantedHeight, heightDamping * Time.deltaTime);
    28.  
    29.     // Convert the angle into a rotation
    30.     currentRotation = Quaternion.Euler (0, currentRotationAngle, 0);
    31.    
    32.     // Set the position of the camera on the x-z plane to:
    33.     // distance meters behind the target
    34.     transform.position = target.position;
    35.     transform.position -= currentRotation * Vector3.forward * distance;
    36.  
    37.     // Set the height of the camera
    38.     transform.position.y = currentHeight;
    39.    
    40.     // Always look at the target
    41.     transform.LookAt (target);
    42. }
    43.  
     
  19. suresh

    suresh

    Joined:
    Mar 29, 2011
    Posts:
    45
    I tried of putting the tag name of the game object in place of the
    but i got an error when i did that thing.
     
  20. suresh

    suresh

    Joined:
    Mar 29, 2011
    Posts:
    45
    For the tail of the snake I am doing like the follow can any one tell me is this the right way or not.

    I have created the prefab for the body parts of the snake with 0 mass(its a rigid body). Then i have created a game object(rigid body) in the scene and attached a hinge joint and connected to the actual snake.
     
  21. Jesse Anders

    Jesse Anders

    Joined:
    Apr 5, 2008
    Posts:
    2,857
    The 'right way' to do it is the way that works correctly and meets your requirements :)

    Does the method you're using work correctly and meet your requirements?
     
  22. suresh

    suresh

    Joined:
    Mar 29, 2011
    Posts:
    45
    Hi there, I think i thought am very near and going to complete snake game but got a problem. Every thing is went wrong suddenly....in the last part(the tail part). when the snake eat food its able to create the tail part but the tail is not following the snake head. The code that i am working on is the bellow one

    Code (csharp):
    1.  
    2. var speed = 6.0;
    3. var food : Transform;
    4. var snake : Transform;
    5. var piece : GameObject;
    6. private var lastPiece : GameObject;
    7.  
    8. function Update ()
    9. {
    10. var controller : CharacterController = GetComponent(CharacterController);
    11. var x = Input.GetAxis("Horizontal") * Time.deltaTime * speed;
    12. var z = Input.GetAxis("Vertical") * Time.deltaTime * speed;
    13. transform.Translate(x, 0, z);
    14. }
    15. function OnCollisionEnter(theCollision : Collision)
    16. {
    17. if(theCollision.gameObject.tag == "Food")
    18.  {
    19.  
    20.   Debug.Log("Hit the Food");
    21.  
    22.   Destroy(theCollision.gameObject);
    23.  
    24.   var position = Vector3(Random.Range(-3,10), 0.284 ,Random.Range(-8, 8));
    25.   Instantiate(food, position, Quaternion.identity);
    26.   addPiece();
    27.   }
    28.   else
    29.   if(theCollision.gameObject.tag == "Wall")
    30. {
    31.   Debug.Log("Snake hit the wall");
    32.   Debug.Log("Snake dead");
    33.   Instantiate(snake, Vector3(Random.Range(-3,10), 0.375 ,Random.Range(-9, 9)), Quaternion.identity);
    34.   Debug.Log("Camera for the Snake");
    35.   gameObject.Find("Main Camera").transform.position = Vector3(-1.283034,16.20548,-0.4933916);
    36.   Debug.Log("Got new Camera for the Snake");
    37.   Destroy(gameObject);
    38.   }
    39. }
    40. function addPiece()
    41.     {
    42.         var newPiece : GameObject = Instantiate(piece, transform.position+(rigidbody.velocity*100),Quaternion.identity);
    43.         newPiece.name = "Piece";
    44.         newPiece.GetComponent("SmoothFollow").target = lastPiece;
    45.         lastPiece = piece;
    46.         }
    47.        
    48.  

    and its giving the run time error some thing like this:


    can some one help me out in solving this problem
     
    Last edited: Apr 13, 2011
  23. suresh

    suresh

    Joined:
    Mar 29, 2011
    Posts:
    45
    @Jesse Anders, can you help me out in this. The above post with code the problem and bellow is how i created the tail part(piece)

    I have created the tail parts as rigid bodies, but didn't use the hinge joint.
     
  24. Jesse Anders

    Jesse Anders

    Joined:
    Apr 5, 2008
    Posts:
    2,857
    Can you post just line 34 of Snake.js? (I won't be able to look at it now, but I can look at it tomorrow if you don't have an answer by then.)
     
  25. suresh

    suresh

    Joined:
    Mar 29, 2011
    Posts:
    45
    thanks for reply

    the 34th line the function call, that is

     
  26. Jesse Anders

    Jesse Anders

    Joined:
    Apr 5, 2008
    Posts:
    2,857
    Hm, I can't really dig through it right now, but I recommend that you try to determine exactly what statement is throwing the exception. It looks like you have a null reference somewhere; try to isolate the problem, and then add debug output, e.g.:

    Code (csharp):
    1. if (some_reference == null) {
    2.     Debug.Log("some_reference is null!");
    3. }
    Where appropriate. This should help you determine which reference is causing the problem.

    I know that's probably not the help you wanted, but with a little work you should be able to figure it out (assuming the problem isn't more involved than it appears). There are only so many references in your script, so by process of elimination you should be able to determine which one is causing the problem. (The error message and line number will usually tell you what you need to know, but if for some reason that doesn't provide enough information, adding some debug output should narrow things down further.)
     
  27. suresh

    suresh

    Joined:
    Mar 29, 2011
    Posts:
    45
    the problem is with this

    that the tail parts are not following the head, what we have to do for that problem
     
  28. Jesse Anders

    Jesse Anders

    Joined:
    Apr 5, 2008
    Posts:
    2,857
    Make sure that 'newPiece' has a SmoothFollow component attached to it. (If it doesn't, you'll get a null reference exception there, which I'm assuming is the problem you're having.)
     
  29. suresh

    suresh

    Joined:
    Mar 29, 2011
    Posts:
    45
    Thank a lot for your reply i got solved that problem....if any problems i will let you know
     
  30. sawfish

    sawfish

    Joined:
    Feb 12, 2011
    Posts:
    314
    I just want to commend Jesse Anders with a (completely fictitious and non-material) Medal of Patience.

    Srsly.
     
  31. Jesse Anders

    Jesse Anders

    Joined:
    Apr 5, 2008
    Posts:
    2,857
    Lol :) Well, we all had to start somewhere, right? ;)
     
  32. suresh

    suresh

    Joined:
    Mar 29, 2011
    Posts:
    45
    Getting some problem with the tail part For the first tail piece its worked fine, but the other tail pieces are not working , they are not following the snake they are going down the floor.

    I figured out one problem for this behavior, for the first piece instance in the inspector view the target is set to snakehead and for the second piece instance the target name in the inspector is set to
    Code (csharp):
    1.  
    2. function addPiece () {
    3.     var newPiece : GameObject = Instantiate(piece, transform.position+Vector3(9, 0.5, 9), Quaternion.identity);
    4.     newPiece.name = "Piece";
    5.     newPiece.GetComponent.<SmoothFollow>().target = lastPiece;
    6.     lastPiece = piece; 
    7. }
    8.  
    the target name is pieces........can any one help me out in this problem. one more thing to add for the above,I have used #pragma strict to check......got some error:BCE0022: Cannot convert 'UnityEngine.Object' to 'UnityEngine.GameObject'
     
    Last edited: Apr 14, 2011
  33. suresh

    suresh

    Joined:
    Mar 29, 2011
    Posts:
    45
    solved the problems, now its working fine......
     
  34. jared101

    jared101

    Joined:
    Jun 6, 2011
    Posts:
    14
    Where can i get Smoothfollow... I dl... island demo now my Unity is not working at all..