Search Unity

  1. We are looking for feedback on the experimental Unity Safe Mode which is aiming to help you resolve compilation errors faster during project startup.
    Dismiss Notice
  2. 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

MonoDevelop does not Accept List despite using Collections.Generic Namespace...

Discussion in 'Scripting' started by Brian2k, Apr 6, 2016.

  1. Brian2k

    Brian2k

    Joined:
    Sep 10, 2015
    Posts:
    17
    Hey Guys,

    I am trying to use a very simple script:
    http://www.theappguruz.com/blog/display-projectile-trajectory-path-in-unity

    But Unity wont allow me to render the game cause it thinks there is no reference to List:
    "Assets/Scripts/shooter.cs(18,17): error CS0246: The type or namespace name `List' could not be found. Are you missing a using directive or an assembly reference?"

    But as you can see in the Source, there is already a namespace for that?!

    Anyone an idea?

    Thanks
     
  2. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,713
    You want to post the code here? The link is broken on mobile devices.
     
  3. Brian2k

    Brian2k

    Joined:
    Sep 10, 2015
    Posts:
    17
    Sure:


    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. public class CannonScript : MonoBehaviour
    5. {
    6. // TrajectoryPoint and Ball will be instantiated
    7.     public GameObject TrajectoryPointPrefeb;
    8.     public GameObject BallPrefb;
    9.  
    10.     private GameObject ball;
    11.     private bool isPressed, isBallThrown;
    12.     private float power = 25;
    13.     private int numOfTrajectoryPoints = 30;
    14.     private List trajectoryPoints;
    15.     //---------------------------------------  
    16.     void Start ()
    17.     {
    18.         trajectoryPoints = new List();
    19.         isPressed = isBallThrown =false;
    20. //   TrajectoryPoints are instatiated
    21.         for(int i=0;i<numOfTrajectoryPoints;i++)
    22.         {
    23.             GameObject dot= (GameObject) Instantiate(TrajectoryPointPrefeb);
    24.             dot.renderer.enabled = false;
    25.             trajectoryPoints.Insert(i,dot);
    26.         }
    27.     }
    28.     //---------------------------------------  
    29.     void Update ()
    30.     {
    31.         if(isBallThrown)
    32.             return;
    33.         if(Input.GetMouseButtonDown(0))
    34.         {
    35.             isPressed = true;
    36.             if(!ball)
    37.                 createBall();
    38.         }
    39.         else if(Input.GetMouseButtonUp(0))
    40.         {
    41.             isPressed = false;
    42.             if(!isBallThrown)
    43.             {
    44.                 throwBall();
    45.             }
    46.         }
    47.     // when mouse button is pressed, cannon is rotated as per mouse movement and projectile trajectory path is displayed.
    48.         if(isPressed)
    49.         {
    50.             Vector3 vel = GetForceFrom(ball.transform.position,Camera.main.ScreenToWorldPoint(Input.mousePosition));
    51.             float angle = Mathf.Atan2(vel.y,vel.x)* Mathf.Rad2Deg;
    52.             transform.eulerAngles = new Vector3(0,0,angle);
    53.             setTrajectoryPoints(transform.position, vel/ball.rigidbody.mass);
    54.         }
    55.     }
    56.     //---------------------------------------  
    57.     // Following method creates new ball
    58.     //---------------------------------------  
    59.     private void createBall()
    60.     {
    61.         ball = (GameObject) Instantiate(BallPrefb);
    62.         Vector3 pos = transform.position;
    63.         pos.z=1;
    64.         ball.transform.position = pos;
    65.         ball.SetActive(false);
    66.     }
    67.     //---------------------------------------  
    68. // Following method gives force to the ball
    69.     //---------------------------------------  
    70.     private void throwBall()
    71.     {
    72.         ball.SetActive(true);  
    73.         ball.rigidbody.useGravity = true;
    74.         ball.rigidbody.AddForce(GetForceFrom(ball.transform.position,Camera.main.ScreenToWorldPoint(Input.mousePosition)),ForceMode.Impulse);
    75.         isBallThrown = true;
    76.     }
    77.     //---------------------------------------  
    78. // Following method returns force by calculating distance between given two points
    79.     //---------------------------------------  
    80.     private Vector2 GetForceFrom(Vector3 fromPos, Vector3 toPos)
    81.     {
    82.         return (new Vector2(toPos.x, toPos.y) - new Vector2(fromPos.x, fromPos.y))*power;
    83.     }
    84.     //---------------------------------------  
    85.     // Following method displays projectile trajectory path. It takes two arguments, start position of object(ball) and initial velocity of object(ball).
    86.     //---------------------------------------  
    87.     void setTrajectoryPoints(Vector3 pStartPosition , Vector3 pVelocity )
    88.     {
    89.         float velocity = Mathf.Sqrt((pVelocity.x * pVelocity.x) + (pVelocity.y * pVelocity.y));
    90.         float angle = Mathf.Rad2Deg*(Mathf.Atan2(pVelocity.y , pVelocity.x));
    91.         float fTime = 0;
    92.      
    93.         fTime += 0.1f;
    94.         for (int i = 0 ; i < numOfTrajectoryPoints ; i++)
    95.         {
    96.             float dx = velocity * fTime * Mathf.Cos(angle * Mathf.Deg2Rad);
    97.             float dy = velocity * fTime * Mathf.Sin(angle * Mathf.Deg2Rad) - (Physics2D.gravity.magnitude * fTime * fTime / 2.0f);
    98.             Vector3 pos = new Vector3(pStartPosition.x + dx , pStartPosition.y + dy ,2);
    99.             trajectoryPoints[i].transform.position = pos;
    100.             trajectoryPoints[i].renderer.enabled = true;
    101.             trajectoryPoints[i].transform.eulerAngles = new Vector3(0,0,Mathf.Atan2(pVelocity.y - (Physics.gravity.magnitude)*fTime,pVelocity.x)*Mathf.Rad2Deg);
    102.             fTime += 0.1f;
    103.         }
    104.     }
    105. }
    Well, I am using to implement this code into mine (Without the functions) but it does not accept the List function...:

    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4. using System.Collections.Generic;
    5.  
    6. public class shooter : MonoBehaviour {
    7.  
    8.  
    9.     GameObject prefab;
    10.  
    11.     // TrajectoryPoint and Ball will be instantiated
    12.     public GameObject TrajectoryPointPrefeb;
    13.     public GameObject BallPrefb;
    14.    
    15.     private GameObject ball;
    16.     private bool isPressed, isBallThrown;
    17.     private float power = 25;
    18.     private int numOfTrajectoryPoints = 30;
    19.     private List trajectoryPoints;
    20.     //---------------------------------------    
    21.     public float timeBetwseenShots = 0.33f;  // Allow 3 shots per second
    22.     private float timestamp;
    23.  
    24.  
    25.     void Start () {
    26.         prefab = Resources.Load ("barrel") as GameObject;
    27.  
    28.         trajectoryPoints = new List();
    29.         isPressed = isBallThrown =false;
    30.  
    31.         //   TrajectoryPoints are instatiated
    32.         for(int i=0;i<numOfTrajectoryPoints;i++)
    33.         {
    34.             GameObject dot = (GameObject) Instantiate(TrajectoryPointPrefeb);
    35.             dot.GetComponent<Renderer>().enabled = false;
    36.             trajectoryPoints.Insert(i,dot);
    37.         }
    38.     }
    39.    
    40.     // Update is called once per frame
    41.     void Update () {
    42.  
    43.        
    44.         if(isBallThrown)
    45.             return;
    46.         if(Input.GetMouseButtonDown(0)))
    47.         {
    48.             isPressed = true;
    49.             if(!ball)
    50.                 createBall();
    51.         }
    52.         else if(Input.GetMouseButtonUp(0))
    53.         {
    54.             isPressed = false;
    55.             if(!isBallThrown)
    56.             {
    57.                 throwBall();
    58.             }
    59.         }
    60.         // when mouse button is pressed, cannon is rotated as per mouse movement and projectile trajectory path is displayed.
    61.         if(isPressed)
    62.         {
    63.             Vector3 vel = GetForceFrom(ball.transform.position,Camera.main.ScreenToWorldPoint(Input.mousePosition));
    64.             float angle = Mathf.Atan2(vel.y,vel.x)* Mathf.Rad2Deg;
    65.             transform.eulerAngles = new Vector3(0,0,angle);
    66.             setTrajectoryPoints(transform.position, vel/ball.GetComponent<Rigidbody>().mass);
    67.         }
    68.  
    69.  
    70.  
    71.     /*    if (Input.GetKey(KeyCode.Space) && Time.time >= timestamp && (Input.GetKeyDown(KeyCode.Space)))
    72.         {
    73.             GameObject barrel = Instantiate(prefab) as GameObject;
    74.             barrel.transform.position = transform.position + Camera.main.transform.forward / 2;
    75.             Rigidbody rb = barrel.GetComponent<Rigidbody>();
    76.             rb.velocity = new Vector3(0,10,8);
    77.             timestamp = Time.time + timeBetweenShots;
    78.         } */
    79.     }
    80.  
     
  4. KyleStaves

    KyleStaves

    Joined:
    Nov 4, 2009
    Posts:
    820
    Bug in the example code you're working with. Looks like it should be List<GameObject> and then in awake change new List(); to new List<GameObject>();
     
    Kiwasi likes this.
  5. Brian2k

    Brian2k

    Joined:
    Sep 10, 2015
    Posts:
    17
    Thats it.. Thanks.. Did not see all the solutions in the forums when I was searching for one. I was sure it will work that way.
     
unityunity