Help im getting this error Assets/MouseManager.cs(38,32): error CS1525: Unexpected symbol `grabbedObject'
using UnityEngine;
using System.Collections;
public class MouseManager : MonoBehaviour {
Rigidbody2D grabbedObject = null;
Vector2 mousePos2D;
float dragSpeed = 4f;
void Update() {
if( Input.GetMouseButtonDown (0)){
Vector3 mouseWorldPos3D = Camera.main.ScreenToWorldPoint(Input.mousePosition);
mousePos2D = new Vector2(mouseWorldPos3D.x, mouseWorldPos3D.y);
}
Vector2 dir = Vector2.zero;
RaycastHit2D hit = Physics2D.Raycast (mousePos2D, dir);
if(hit && hit.collider && hit.collider.GetComponent<Rigidbody2D>() != null) {
hit.collider.GetComponent<Rigidbody2D>().gravityScale = 1;
}
if( Input.GetMouseButtonDown (0)){
grabbedObject = null;
}
}
void FixedUpdate() {
if(grabbedObject != null) {
Vector3 mouseWorldPos3D = Camera.main.ScreenToWorldPoint(Input.mousePosition);
mousePos2D = new Vector2(mouseWorldPos3D.x, mouseWorldPos3D.y);
Vector2 dir = mousePos2D - grabbedObject.position;
dir *= dragSpeed
grabbedObject.velocity = dir;
}
}
}
Please edit your question, highlight your code and use the 101010 button to format it properly.
$$anonymous$$uch, thank you. :) Also, use Comments for back and forth communication, and Answers to answer questions. I converted your answer to a comment for you.
Answer by Dave-Carlile · Aug 27, 2015 at 04:35 PM
On line 36...
dir *= dragSpeed
You're missing a semicolon.
I would suggest that you find a basic C# programming tutorial and learn the language syntax. That will help you spot these sorts of errors more quickly so you can move on to more interesting things. :)
It's a little hard to say from just looking at your code. One thing that's odd is that at the end of Update
you're setting grabbedObject = null
if the mouse button is down. Even if the code before it identified an object, you're clearing that out.
Also, take a look at Debug.Log
if you're not familiar with it. It's very useful for logging things out to verify they are what you think they are. You can make sure your raycast is hitting an object by logging it and things like that.
Answer by tauqeerahmed · Aug 27, 2015 at 08:33 PM
You are missing a semicolon that's why u are getting this error
Your answer
![](https://koobas.hobune.stream/wayback/20220612001647im_/https://answers.unity.com/themes/thub/images/avi.jpg)