Search Unity

Variable does not exist in the current context

Discussion in 'Scripting' started by Avo, Apr 6, 2011.

  1. Avo

    Avo

    Joined:
    Dec 4, 2010
    Posts:
    237
    Im trying to rig up one of my variables to the transform of the A* AI project so the enemy will follow the player when hes spawned but it keeps generating the error
    AIFollow.cs(31,36): error CS0103: The name `Dungeon_generation' does not exist in the current context

    I highlighted any relevant text in red/omitted some irrelevant stuff.

    Edit: Found the problem thanks to some help. Its not passing the variables. Does anyone know an easy way to do this? I need to pass from a java script to C#.

    The two relevant scripts are
    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5.  
    6. public class AIFollow : MonoBehaviour {
    7.     //[RequireComponent (typeof(CharacterController))]
    8.     public float speed = 3.0F;
    9.     public float rotationSpeed = 5.0F;
    10.     public float pickNextWaypointDistance = 3.0F;
    11.     public float SearchWaypointFrequency = 0.2F;
    12.     public float maxStop = 3;
    13.    
    14.     public bool continousTargetSearch = false;
    15.     public float targetSearchFrequency = 1.0F;
    16.     private bool canSearchAgain = true;
    17.     public Command command = Command.Stay;
    18.    
    19. [COLOR="red"]   public Transform target;[/COLOR]
    20.     private CharacterController controller;
    21.     private AIAnimation animator;
    22.     private Seeker seeker;
    23.  
    24.     // Make sure there is always a character controller
    25.    
    26.     public enum Command {
    27.         Stay,
    28.         Walk
    29.     }
    30.    
    31.     public IEnumerator Start () {
    32. [COLOR="red"]       Transform target = Theplayer.Dungeon_generation;[/COLOR]
    33.         waypointPosition = transform.position;
    34.         command = Command.Stay;
    35.         controller = GetComponent (typeof(CharacterController)) as CharacterController;
    36.         Object anim = GetComponent (typeof(AIAnimation));
    37.         animator = anim != null ? anim as AIAnimation : null;
    38.         seeker = GetComponent (typeof(Seeker)) as Seeker;
    39.        
    40.         StartCoroutine (Patrol());
    41.         yield return new WaitForSeconds (Random.value*0.5F);
    42.        
    43.         if (continousTargetSearch) {
    44.             StartCoroutine (SearchPlayer());
    45.         }
    46.        
    47.         while (true) {
    48.             FindPoint (curpoint);
    49.             yield return new WaitForSeconds (SearchWaypointFrequency);
    50.         }
    51.     }
    52.    
    53.     private Vector3 waypointPosition;
    54.     //private bool continuous = false;
    55.     private Vector3[] points;
    56.     private int curpoint = 0;
    57.    
    58.     public void Update () {
    59.         //Debug.color = Color.blue;
    60.         Debug.DrawLine (transform.position, waypointPosition, Color.blue);
    61.     }
    62.    
    63.     public IEnumerator SearchPlayer () {
    64.         yield return 0;
    65.         while (true) {
    66.             yield return 0;
    67.             while (!canSearchAgain) {
    68.                 yield return 0;
    69.             }
    70.             if (continousTargetSearch) {
    71.                 canSearchAgain = false;
    72.                 seeker.StartPath (transform.position,target.position);
    73.             }
    74.             yield return new WaitForSeconds (targetSearchFrequency);
    75.         }
    76.        
    77.     }
    78.    
    79.     public void PathComplete (Vector3[] newPoints) {
    80.         canSearchAgain = true;
    81.         points = newPoints;
    82.         FindPoint (0);
    83.         command = Command.Walk;
    84.     }
    85.    
    86.     public void PathError () {
    87.         canSearchAgain = true;
    88.     }
    89.    
    90.     public bool HasReachedTarget () {
    91.         return curpoint >= points.Length;
    92.     }
    93.    
    94.     public void FindPoint (int cpoint) {
    95.         curpoint = cpoint;
    96.         if (points == null || points.Length == 0 || curpoint >= points.Length) {   
    97.             waypointPosition = transform.position;
    98.             Stop ();
    99.             return;
    100.         }
    101.        
    102.         if (points.Length == 1) {
    103.             waypointPosition = points[0];
    104.             command = Command.Walk;
    105.             return;
    106.         }
    107.        
    108.         command = Command.Walk;
    109.        
    110.         waypointPosition = points[curpoint];
    111.         Vector3 p = waypointPosition;
    112.         p.y = transform.position.y;
    113.        
    114.         if (curpoint < points.Length - 1) {
    115.             if ((transform.position-p).sqrMagnitude < pickNextWaypointDistance*pickNextWaypointDistance) {
    116.                 curpoint++;
    117.                 FindPoint (curpoint);
    118.             }
    119.            
    120.         } else {
    121.             if ((transform.position-p).sqrMagnitude < maxStop*maxStop) {
    122.                 curpoint++;
    123.                 FindPoint (curpoint);
    124.             }
    125.         }
    126.     }
    127.    
    128.     public IEnumerator Patrol () {
    129.         while (true) {
    130.             if (command == Command.Walk) {
    131.                 MoveTowards(waypointPosition);
    132.             }
    133.            
    134.             yield return 0;
    135.         }
    136.     }
    137.    
    138.     public void Stop () {
    139.         command = Command.Stay;
    140.         if (animator != null) {
    141.             animator.SetSpeed (0.0F);
    142.         }
    143.     }
    144.    
    145.     public void RotateTowards (Vector3 position) {
    146.         if (animator != null) {
    147.             animator.SetSpeed (0.0F);
    148.         }
    149.        
    150.         Vector3 direction = position - transform.position;
    151.         direction.y = 0;
    152.        
    153.         if (curpoint == points.Length - 1  direction.sqrMagnitude < maxStop*maxStop) {
    154.             FindPoint (curpoint);
    155.             return;
    156.         }
    157.        
    158.         if (direction.sqrMagnitude < 0.1F*0.1F) {
    159.             return;
    160.         }
    161.        
    162.         // Rotate towards the target
    163.         transform.rotation = Quaternion.Slerp (transform.rotation, Quaternion.LookRotation(direction), rotationSpeed * Time.deltaTime);
    164.         transform.eulerAngles = new Vector3(0, transform.eulerAngles.y, 0);
    165.     }
    166.    
    167.     public void MoveTowards (Vector3 position) {
    168.         Vector3 direction = position - transform.position;
    169.         direction.y = 0;
    170.        
    171.         if (direction.sqrMagnitude < 0.2F*0.2F) {
    172.             Stop ();
    173.             return;
    174.         }
    175.        
    176.         // Rotate towards the target
    177.         transform.rotation = Quaternion.Slerp (transform.rotation, Quaternion.LookRotation(direction), rotationSpeed * Time.deltaTime);
    178.         transform.eulerAngles = new Vector3(0, transform.eulerAngles.y, 0);
    179.    
    180.         // Modify speed so we slow down when we are not facing the target
    181.         Vector3 forward = transform.TransformDirection(Vector3.forward);
    182.         float speedModifier = Vector3.Dot(forward, direction.normalized);
    183.         speedModifier = Mathf.Clamp01(speedModifier);
    184.        
    185.         // Move the character
    186.         if (controller) {
    187.             direction = forward * speed * speedModifier*speedModifier;
    188.             controller.SimpleMove(direction);
    189.         } else {
    190.             direction = forward * speed * speedModifier*speedModifier;
    191.             transform.Translate (direction*Time.deltaTime,Space.World);
    192.         }
    193.        
    194.         if (animator != null) {
    195.             animator.SetSpeed (speed * speedModifier);
    196.         }
    197.     }
    198. }
    And

    Code (csharp):
    1.  
    2. import System.IO;
    3.  
    4. //setup the variables.
    5. var rows = 100;
    6. var cols = 100;
    7. var dungeon = new int[cols,rows];
    8. var oldcol:int;
    9. var oldrow:int;
    10. var armlength:int;
    11. var roomheight;
    12. var roomwidth;
    13. var newcol:int;
    14. var newrow:int;
    15. var chance=1.0;      //this variable increments to decease the chance of hallways spawning the more hallways there are.
    16. [COLOR="red"]var Theplayer:Transform; [/COLOR]
    17.  
    18. // set up the constants
    19. var  maxroomheight:int=7;
    20. var minroomheight:int=3;
    21. var maxroomwidth:int=7;
    22. var minroomwidth:int=3;
    23. var hallway : Transform;
    24. var wall : Transform;
    25. var Hero : Transform;
    26. // Different variables in the dungeon array mean different things.
    27. //  0 is occupied by a wall,  1 is open space and 2 is a node for the creation of new hallways.
    28.  
    29.  
    30. function Start()
    31. {
    32.     //Clear the dungeon.
    33.     for(d=0; d<cols; d++)
    34.     {
    35.         for(e=0; e<rows; e++)
    36.         {
    37.             dungeon[d,e]=0;
    38.         }
    39.     }
    40.    
    41.    
    42.     //pick the seed tile for the dungeon and remember it as the start position;
    43.     oldcol=Random.Range((cols/4),(cols-(cols/4)));
    44.     oldrow=Random.Range((rows/4),(rows-(rows/4)));
    45.     newcol=oldcol;
    46.     newrow=oldrow;
    47.     dungeon[oldcol,oldrow]=2;
    48.     //place the player at the start position.
    49. [COLOR="red"]   The_player = Instantiate (Hero, Vector3((oldcol*5),1,(oldrow*5)), Quaternion.identity);[/COLOR]
    50.    
    51.    
    52.     //Find a tile thats been flagged as a node.
    53.     for(f=0; f<25; f++)
    54.     {
    55.         for(d=0; d<cols; d++)
    56.         {
    57.             for(e=0; e<rows; e++)
    58.             {
    59.                 if (dungeon[d,e]==2)
    60.                 {
    61.                     oldcol=d;
    62.                     oldrow=e;
    63.                     buildarm(oldcol,oldrow);
    64.                 }
    65.             }
    66.         }
    67.     }
    68.    
    69.  
    70.    
    71.     //build the dungeon from cubes and hallway objects.
    72.     for (x=0; x<cols; x++)
    73.     {
    74.         for(z=0; z<rows; z++)
    75.         {
    76.             if (dungeon[x,z]>0)
    77.             {
    78.                 Instantiate (hallway, Vector3(x*5, 0, z*5), Quaternion.identity);
    79.             }
    80.             else
    81.             {
    82.                 Instantiate (wall, Vector3(x*5,2.5, z*5), Quaternion.identity);
    83.             }
    84.         }
    85.     }
    86.    
    87.     //Call the scan function for the AI.
    88.     AstarPath.active.Scan ();
    89. }
    90.  
    91.  
    92.  
     
    Last edited: Apr 6, 2011
  2. dbp

    dbp

    Joined:
    Mar 3, 2010
    Posts:
    324
    I can't see what "Theplayer" is, is it the name of another script? Also, where do you get the variable "Dungeon_generation" from? I don't see it mentioned anywhere but in that line.
     
  3. Avo

    Avo

    Joined:
    Dec 4, 2010
    Posts:
    237
    Dungeon_Generation is the name of the other script, "ThePlayer" is the variable holding my player object. Theplayer is declared/assigned its value in the second script. Im trying to assign a variable in the first script to theplayer.
     
  4. sebako

    sebako

    Joined:
    Jun 27, 2009
    Posts:
    301
    you need to access the second script first before you can access its vars, if it is attached to the same gameobject you can just call
    this.getComponent(secondscriptnamehere).Theplayer.Dungeon_generation
     
  5. Avo

    Avo

    Joined:
    Dec 4, 2010
    Posts:
    237
    Unfortunately they arnt/cant be attached to the same game object. Do you know of another way? Im going to dig around through the get component API a little myself.
     
  6. Avo

    Avo

    Joined:
    Dec 4, 2010
    Posts:
    237
    Oh and Thanks!/Bump!
     
  7. Avo

    Avo

    Joined:
    Dec 4, 2010
    Posts:
    237
    X-treme bump. I really need help with passing this variable. Is there any more information i could give to make it easier on you guys?