• 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 shepsaus000 · Oct 28, 2015 at 03:16 PM · scripting problem

Getting compiler errors with random spawning code

So I'm trying to get a ball to spawn at a random location at the top of my 9:16 screen in my 2D game in Unity, but this code is giving me some compiler errors. The compiler errors involve mainly the 26th line (the instantiate line with the random.range code). The first one tells me that the expression denotes a type, where a variable, value, or method group was expected. The second one says that no overload for method 'Instantiate' takes '2' arguments. Any ideas on how to fix this? Here is my code:

 using UnityEngine;
 using System.Collections;
 
 public class Spawning : MonoBehaviour
 {
     public GameObject ball;
     public GameObject platform;
     public float spawnRate;
     public int ballCount;
     public int platformCount;
     public bool ability;
 
 
 
     void Start () {
         spawnRate = 0;
         ballCount = 1;
         platformCount = 1;
         ability = true;
     }
     
 
     void Update () {
         spawnRate += Time.deltaTime;
         if (spawnRate == 20 && ability == true) {
             Instantiate (ball, Vector3 (Random.Range (0, 16), Random.Range (7, 9), Quaternion.identity));
             spawnRate = 0;
             ballCount += 1;
         }
 
     }
 }
 

Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by tarasfromlviv · Oct 28, 2015 at 03:18 PM

must be new Vector3(...) in first place which is the constructor of the struct, next thing you are closing braces in wrong place.

Instantiate (ball, new Vector3 (UnityEngine.Random.Range (0, 16), UnityEngine.Random.Range (7, 9)), Quaternion.identity); - this is the line that's correct

http://docs.unity3d.com/ScriptReference/Vector3-ctor.html

Comment
Add comment · Show 3 · Share
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 shepsaus000 · Oct 28, 2015 at 03:39 PM 0
Share

Thanks a ton! Do you $$anonymous$$d explaining this to me a bit? You see, I'm new to Unity and I've been learning through the good-old fashioned way: mimicry. Of course, it would be nice to get some knowledge on why I'm doing the things I'm doing.

avatar image tarasfromlviv shepsaus000 · Oct 28, 2015 at 03:50 PM 0
Share

This is just C# basics, you should get comfortable with it a little bit before diving into scripting. This is the awesome place to start https://unity3d.com/learn/tutorials/topics/scripting These are very basic things like invoking a constructor and method with parameters, so do your math and learn some C# ;)

avatar image shepsaus000 tarasfromlviv · Oct 28, 2015 at 03:56 PM 0
Share

Thanks a ton man! I'll get on this right away!

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

32 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Casting a GraphView node using GetNodeByGuid() returns null 1 Answer

How to to call a method from UI trigger event? 0 Answers

2D gun shooting and fliping 1 Answer

Cannot flip sprite when Z rotation >= 90 & <= -90 1 Answer

Access to a scene that has just been loaded 1 Answer


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