Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by sid4 · Oct 02, 2016 at 05:38 AM · scripting problem

Please help ! I keep getting this error with my script.

I keep getting this error code and game wont play -the error code is-

"Assets/darts/CannonScript.cs(17,25): error CS0246: The type or namespace name `List' could not be found. Are you missing a using directive or an assembly reference?" the code look good so whats going on? please help

using UnityEngine; using System.Collections; using System.Collections.Generic;

     public class CannonScript : MonoBehaviour
     {
         // TrajectoryPoint and Ball will be instantiated
         public GameObject TrajectoryPointPrefeb;
         public GameObject BallPrefb;
 
         private GameObject ball;
         private bool isPressed, isBallThrown;
         private float power = 25;
         private int numOfTrajectoryPoints = 30;
         private List trajectoryPoints;
         //---------------------------------------    
         void Start ()
         {
             trajectoryPoints = new List();
             isPressed = isBallThrown =false;
             //   TrajectoryPoints are instatiated
             for(int i=0;i<numOfTrajectoryPoints;i++)
             {
                 GameObject dot= (GameObject) Instantiate(TrajectoryPointPrefeb);
                 dot.renderer.enabled = false;
                 trajectoryPoints.Insert(i,dot);
             }
         }
         //---------------------------------------    
         void Update ()
         {
             if(isBallThrown)
                 return;
             if(Input.GetMouseButtonDown(0))
             {
                 isPressed = true;
                 if(!ball)
                     createBall();
             }
             else if(Input.GetMouseButtonUp(0))
             {
                 isPressed = false;
                 if(!isBallThrown)
                 {
                     throwBall();
                 }
             }
             // when mouse button is pressed, cannon is rotated as per mouse movement and projectile trajectory path is displayed.
             if(isPressed)
             {
                 Vector3 vel = GetForceFrom(ball.transform.position,Camera.main.ScreenToWorldPoint(Input.mousePosition));
                 float angle = Mathf.Atan2(vel.y,vel.x)* Mathf.Rad2Deg;
                 transform.eulerAngles = new Vector3(0,0,angle);
                 setTrajectoryPoints(transform.position, vel/ball.rigidbody.mass);
             }
         }
         //---------------------------------------    
         // Following method creates new ball
         //---------------------------------------    
         private void createBall()
         {
             ball = (GameObject) Instantiate(BallPrefb);
             Vector3 pos = transform.position;
             pos.z=1;
             ball.transform.position = pos;
             ball.SetActive(false);
         }
         //---------------------------------------    
         // Following method gives force to the ball
         //---------------------------------------    
         private void throwBall()
         {
             ball.SetActive(true);    
             ball.rigidbody.useGravity = true;
             ball.rigidbody.AddForce(GetForceFrom(ball.transform.position,Camera.main.ScreenToWorldPoint(Input.mousePosition)),ForceMode.Impulse);
             isBallThrown = true;
         }
         //---------------------------------------    
         // Following method returns force by calculating distance between given two points
         //---------------------------------------    
         private Vector2 GetForceFrom(Vector3 fromPos, Vector3 toPos)
         {
             return (new Vector2(toPos.x, toPos.y) - new Vector2(fromPos.x, fromPos.y))*power;
         }
         //---------------------------------------    
         // Following method displays projectile trajectory path. It takes two arguments, start position of object(ball) and initial velocity of object(ball).
         //---------------------------------------    
         void setTrajectoryPoints(Vector3 pStartPosition , Vector3 pVelocity )
         {
             float velocity = Mathf.Sqrt((pVelocity.x * pVelocity.x) + (pVelocity.y * pVelocity.y));
             float angle = Mathf.Rad2Deg*(Mathf.Atan2(pVelocity.y , pVelocity.x));
             float fTime = 0;
 
             fTime += 0.1f;
             for (int i = 0 ; i < numOfTrajectoryPoints ; i++)
             {
                 float dx = velocity * fTime * Mathf.Cos(angle * Mathf.Deg2Rad);
                 float dy = velocity * fTime * Mathf.Sin(angle * Mathf.Deg2Rad) - (Physics2D.gravity.magnitude * fTime * fTime / 2.0f);
                 Vector3 pos = new Vector3(pStartPosition.x + dx , pStartPosition.y + dy ,2);
                 trajectoryPoints[i].transform.position = pos;
                 trajectoryPoints[i].renderer.enabled = true;
                 trajectoryPoints[i].transform.eulerAngles = new Vector3(0,0,Mathf.Atan2(pVelocity.y - (Physics.gravity.magnitude)*fTime,pVelocity.x)*Mathf.Rad2Deg);
                 fTime += 0.1f;
             }
         }
     }
Comment
Add comment · Show 3
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image gjf · Oct 01, 2016 at 02:54 PM 0
Share

you should check the syntax of List. you need to define what object type you want it to comprise of.

avatar image sid4 gjf · Oct 01, 2016 at 03:10 PM 0
Share

what do you mean check the syntax? and do you mean I put the ball in the list? since the ball is the object to be moved and projected

avatar image gjf sid4 · Oct 01, 2016 at 10:35 PM 0
Share

syntax relates to how you use a command...

in this instance i'm suggesting that you check the use of the List command in your code. line 17 is where the problem exists (from the error message, but line 15 as posted because not all of your code is formatted properly).

to resolve this you need to decide what type of object the List contains. if this is still confusing then the best advice that i can give is to follow some C# tutorials which explain the fundamentals of the language. time spent learning it now will save a lot of headaches in the future...

finally, for your use case, you should use List.Add() ins$$anonymous$$d of Insert. again, check the syntax for specific use.

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Ways to make OnMouseDown (or a single script) differentiate between different colliders/sprites 1 Answer

How can I make that the waiting time in StartCoroutine will take effect when changing the value in inspector when the game is running ? 1 Answer

variable not being changed outside of a method. [C#] 3 Answers

Look On Cursor Problem 1 Answer

Setting a random animation frame from multiple animations 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges