Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 Nabil_fx · Dec 18, 2014 at 02:43 PM ·

Script help!!!

Hi put this script in c#, but there is an error (A namespace can only contain types and namespace declarations). How can i fix that?

 void Update () {
          
     // test for touch 
     if (Input.touchCount > 0) {
               
         // grab reference to this touch
         Touch userTouch = Input.GetTouch (0);
  
         // ray from cameara to point of finger touch
         Ray ray = Camera.main.ScreenPointToRay(userTouch.position);
  
         // hit object to record details of what was hit (touched)
         RaycastHit hit;
  
         // if we got a hit and it was on one of the gameobject
         if (Physics.Raycast (ray, out hit) && hit.collider.gameObject.name == "name of gameobject" ) {
  
             if (userTouch.phase == TouchPhase.Began) {
  
             }
             else if (userTouch.phase == TouchPhase.Moved) {
              
             }
             else if (userTouch.phase == TouchPhase.Ended || userTouch.phase == TouchPhase.Canceled) {
              
             }
         }
     }
 }   
Comment
Add comment · Show 5
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 bkevelham · Dec 18, 2014 at 02:44 PM 1
Share

This isn't your full script, is it? Care to include the rest of your class?

avatar image MakinStuffLookGood · Dec 18, 2014 at 03:50 PM 1
Share

It would also be helpful to see a copy/paste of the error message.

avatar image Nabil_fx · Dec 18, 2014 at 03:58 PM 0
Share

here is the error.

A namespace can only contain types and namespace declarations

avatar image MakinStuffLookGood · Dec 18, 2014 at 04:01 PM 0
Share

Nothing in this code snippet is causing that, so as bkevelham mentioned, we'd need to see the rest of the class to help out.

avatar image Nabil_fx · Dec 18, 2014 at 04:03 PM 0
Share

i put this script in C#, is for raycast an 3D object.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by MakinStuffLookGood · Dec 18, 2014 at 04:27 PM

You cannot have your update method outside of a class, more specifically, outside of a class inheriting MonoBehaviour.

Your code needs to be in a MonoBehaviour, and then that Monobehaviour needs to be put onto a GameObject for it to do anything in your game.

 using UnityEngine;
 using System.Collections;

 public class TouchRaycaster : MonoBehaviour
 {

     // Paste your Update method in here.

 }

This is really fundamental stuff you ought to know before delving into the kinds of things it looks like you're trying to do. I suggest you do some intro tutorial stuff for both Unity and C# in general before getting into a game with touch inputs and raycasting onto 3D objects

Comment
Add comment · Show 1 · 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 Nabil_fx · Dec 18, 2014 at 05:42 PM 0
Share

Here is the Unity package project, that have the problem. There is a Cube, with a Touchobjectevent, from Playmaker, thatch to a Ngui, to get the right position on every smartphone screen. Please if u have a little time to look the project, and see whats wrong, cause when i touch the Cube button where is attach to the Ngui, it does not work. It suppose when the Cube is touch, the Cube runs an animation to the left them came back to the original position. project Link:

http://we.tl/mf5WQbDFmN

Needs playmaker and Ngui

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

A node in a childnode? 1 Answer

respawn at current position 0 Answers

How do I make the YEI 3-Space Sensor Bluetooth work with unity? 2 Answers

How to simplify my Equipment method? 1 Answer

How To Add A Sprint Function? 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