Search Unity

  1. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice
  2. Ever participated in one our Game Jams? Want pointers on your project? Our Evangelists will be available on Friday to give feedback. Come share your games with us!
    Dismiss Notice

Problem with Array

Discussion in 'Scripting' started by henry96, Aug 11, 2013.

  1. henry96

    henry96

    Joined:
    Sep 28, 2011
    Posts:
    582
    Hello everyone. I have spent a couple of hours trying to get this simple code works. But it is just not working.

    mover.js

    Code (csharp):
    1. #pragma strict
    2.  
    3. var target = new Array ();
    4.  
    5. function Start () {
    6.    
    7.     //Access the waypoint
    8.     var waypoint = GameObject.FindWithTag("waypoint").GetComponent(Waypoint).waypoint;
    9.    
    10.     for (var i = 0; i < waypoint.length; i++)   {
    11.    
    12.         target.Add (waypoint[i].gameObject);
    13.        
    14.        
    15.     }
    16. }
    17.  
    18. function Update () {
    19.            
    20.         //Look at the target point
    21.         transform.LookAt (target[0].transform.position);   
    22.                    
    23.            
    24. }
    25.    
    26. }
    Waypoint.js

    Code (csharp):
    1.  
    2. var waypoint : Transform[];
    3.  
    I try to make the object look at one of the target. However, console indicates transform is not a member of object. I tried to change it to gameObject. But still no luck. Please help me out with this. Thanks in advance
     
  2. HyenaGamesDev

    HyenaGamesDev

    Joined:
    Aug 4, 2013
    Posts:
    32
    I'm still a Unity newb, and I don't know javascript at all. so I'll try to help in general terms.

    First, make sure that your GameObject.FindWithTag() line is working. Make sure it doesn't return Null.

    If that doesn't help, try to simplify things. Get rid of the Array and Loop temporarily. Make your Start() function just set a transform. and your Update() function look at that transform. If that doesn't work, then you have other issues besides the array.

    From what I understand, transform should be a part of Every gameobject and everything that derives from it.

    If this doesn't help, Post a bit more of your code and the exact compiler or console errors you get. Thanks.
     
  3. sam-perisentient

    sam-perisentient

    Joined:
    Aug 11, 2013
    Posts:
    31
    The error from your code would have been: 'transform' is not a member of 'Object'. This is because Array contains Objects, not GameObjects. A cast is required. Change your update function to this:
    Code (csharp):
    1.  
    2. function Update () {
    3.     //Look at the target point
    4.     var looktarget = target[0] as GameObject;
    5.     transform.LookAt(looktarget.transform.position);
    6. }
    7.  
     
  4. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,240
    You should not use this:

    Code (csharp):
    1. var target = new Array ();
    Use a generic List instead. Then you won't have casting problems, and it will run faster.

    --Eric
     
  5. henry96

    henry96

    Joined:
    Sep 28, 2011
    Posts:
    582
    Having "as GameObject" at the end makes it work now. Thank you.

    I will look into that as well. Thanks for your recommendation.
     
unityunity