- Home /
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) {
}
}
}
}
This isn't your full script, is it? Care to include the rest of your class?
It would also be helpful to see a copy/paste of the error message.
here is the error.
A namespace can only contain types and namespace declarations
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.
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
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:
Needs playmaker and Ngui
Your answer
Follow this Question
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