- Home /
Error: not a member of 'UnityEngine.GameObject[]'.
Hi the code below is giving me this error and I can't seem to figure out why I think its from assigning something with the array wrong but can' t figure why it gives me the error below?
Code:
 #pragma strict
 
 var birdAnimator : Animator;
 var electricityParticleGameObject : GameObject;
 var scoreValue = 1;
 var script : Move_Left_Script;
 var scoreControlScript : Score_Control_Script;
 var coinBurstParticles : GameObject;
 var magnetGlowParticles : GameObject;
 var coins : GameObject[];
 var trigger = false;
 var speed : float;
 
 
 function Start ()
 {
     for (var i : int = 0; i < 7; i++)
     {
         coins = GameObject.FindGameObjectsWithTag("Coin");
     }
 }
 
 function OnTriggerEnter2D(other : Collider2D)
 {
     
     if (other.tag == "EvilCloud")
     {
     
 //        script.pauseEvilCloudTrigger = true;
         // Activate bird shocking animation
         birdAnimator.enabled = true;
         // Trigger for update function to make sure bird hit cloud before it runs electricity shocking effect
         electricityParticleGameObject.active = true;
         // Destroy Bounce Controller Script on Bird so it disables bouncing from the players input
         Destroy(GetComponent(Bounce_Controller_Script));
         // Destroy Rigidbody so Bird doesn't fall right away
         ShockPauseFall();
         // Set High Score
         scoreControlScript.SetHighScore();
         
         Debug.Log("Player hit evil cloud. End Game!");
     }
     
     if (other.tag == "Coin")
     {
         // Spawns coin burst particles at the location of where the coin was hit and then destroys the particle system after 2 seconds
         var coinBurstClone : GameObject = Instantiate(coinBurstParticles, other.transform.position, Quaternion.identity);
         Destroy(coinBurstClone, 2);
         // Add a point to the current score when coin is hit and also update high score if its lower then current score
         // then disable renderer and collider2D and it gets renabled when it repostions in Coin Spawner Script
         Score_Control_Script.currentScore += scoreValue;
         other.gameObject.renderer.enabled = false;
         other.gameObject.collider2D.enabled = false;
         
     }
     
     if (other.tag == "Magnet")
     {
         magnetGlowParticles.active = true;
         other.gameObject.renderer.enabled = false;
         other.gameObject.collider2D.enabled = false;
         trigger = true;
         Debug.Log("Magnet hit bring that ish!");
         }
 }
 
 function Update () 
 {
     var step = speed * Time.deltaTime;
     
     if (trigger == true)
     {
          coins.transform.position = Vector3.MoveTowards(coins.transform.position, gameObject.transform.position, step);
     }
 }
 
 function ShockPauseFall () 
 {
     // Destroys rigidbody2D component so bird pauses then waits 2 seconds then adds a rigidbody2d and sets its gravity to 3 so it falls fast
     Destroy(GetComponent(Rigidbody2D));
         
 }
Error:
 Assets/Scripts/Gameplay Functionality Script.js(75,23): BCE0019: 'transform' is not a member of 'UnityEngine.GameObject[]'. 
 Assets/Scripts/Gameplay Functionality Script.js(75,70): BCE0019: 'transform' is not a member of 'UnityEngine.GameObject[]'. 
Answer by Jammer3000 · Jun 18, 2014 at 03:50 PM
This solved the problem thanks to @tanoshimi for the answer!
 if (trigger == true)
     {
         for(var coin : GameObject in coins){
             coin.transform.position = Vector3.MoveTowards(coin.transform.position, gameObject.transform.position, step);
         }
     }
Answer by Kiwasi · Jun 18, 2014 at 01:26 AM
On line 73 you call coins.transform
coins is a GameObject[]. Arrays do not have a transform.
To access the transform you need to call coins[i].transform where i is the index of the coin you want to access.
Another way to do this is with foreach (c# code)
 foreach (GameObject coin in coins){
     coin.transform. //do whatever
 }
(As an aside your for loop in the start function is doing you no good. Removing line 17 will give you the exact same result faster.)
If I remove the for loop how will I access the coins index by doing coins[i]??
The i in coins[i] is the coins index. http://unity3d.com/learn/tutorials/modules/beginner/scripting/arrays 
I did what you said deleted the loop and changed the coins.transform to coins[i].transform and now it gives me these two new errors?
 Assets/Scripts/Gameplay Functionality Script.js(74,23): BCE0005: $$anonymous$$ identifier: 'i'.
 
 Assets/Scripts/Gameplay Functionality Script.js(74,73): BCE0005: $$anonymous$$ identifier: 'i'.
Well, which coin did you want to move towards?! You need to supply its index from the coins array.
So then in Update() you want:
 if (trigger == true)
 {
     for(var coin : GameObject in coins){
         coin.transform.position = Vector3.$$anonymous$$oveTowards(coin.transform.position, gameObject.transform.position, step);
     }
 }
Your answer
 
 
              koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                