- Home /
Hey Idk but why does this script not work it#s a hover script
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class hover : MonoBehaviour {
public TextMesh Text1;
public TextMesh Text2;
public GameObject HoverObject;
// Use this for initialization
void Start () {
Text1.text = "New Game";
Text2.text = "Continue";
}
// Update is called once per frame
void Update () {
}
void OnMouseOver() {
Hover ();
if (GameObject = Newgame) {
HoverObject = "Newgame";
}
if (GameObject = Continue) {
HoverObject = "Continue";
}
}
void OnMouseExit() {
Exit ();
}
public void Hover() {
if (HoverObject = "Newgame") {
print ("test");
}
if (HoverObject = "Continue") {
print ("test1");
}
}
public void Exit() {
}
}
this is the hirkey 
its giving me these errors:
Assets/hover.cs(25,20): error CS0103: The name Newgame' does not exist in the current context Assets/hover.cs(25,7): error CS0118: UnityEngine.GameObject' is a type' but a variable' was expected
Assets/hover.cs(10,9): error CS0131: The left-hand side of an assignment must be a variable, a property or an indexer
Assets/hover.cs(26,18): error CS0029: Cannot implicitly convert type string' to UnityEngine.GameObject'
Assets/hover.cs(29,20): error CS0103: The name Continue' does not exist in the current context Assets/hover.cs(29,7): error CS0118: UnityEngine.GameObject' is a type' but a variable' was expected
Assets/hover.cs(30,18): error CS0029: Cannot implicitly convert type string' to UnityEngine.GameObject'
Assets/hover.cs(40,21): error CS0029: Cannot implicitly convert type string' to UnityEngine.GameObject'
Assets/hover.cs(44,21): error CS0029: Cannot implicitly convert type string' to UnityEngine.GameObject'
Answer by ryanmillerca · Jul 19, 2019 at 04:49 PM
These if statements are formatted wrong.
if (HoverObject = "Newgame") {
should be
if (HoverObject == "Newgame") {
a single equals sign = sets a variable to a new value. Two equals signs == compares whether values are equal. Also, HoverObject is a gameobject, and will never be equal to "Newgame" or any other typed string. You should compare it's name. if (HoverObject.name == "Newgame"). I think you would benefit from studying C# basics - as Unity answers can be a little unforgiving for high volumes of beginner mistakes. Check out the Unity Learn section, or other various C# tutorials on the internet to improve your skills.
yeah i'm just learning and im using the unity forums to help me
now im getting:
Assets/hover.cs(25,18): error CS0120: An object reference is required to access non-static member UnityEngine.Object.name' Assets/hover.cs(26,18): error CS0029: Cannot implicitly convert type string' to UnityEngine.GameObject' Assets/hover.cs(29,18): error CS0120: An object reference is required to access non-static member UnityEngine.Object.name'
Assets/hover.cs(30,18): error CS0029: Cannot implicitly convert type string' to UnityEngine.GameObject'
New Code:
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class hover : $$anonymous$$onoBehaviour {
public Text$$anonymous$$esh Text1;
public Text$$anonymous$$esh Text2;
public GameObject HoverObject;
// Use this for initialization
void Start () {
Text1.text = "New Game";
Text2.text = "Continue";
}
// Update is called once per frame
void Update () {
}
void On$$anonymous$$ouseOver() {
Hover ();
if (GameObject.name == "Newgame") {
HoverObject = "Newgame";
}
if (GameObject.name == "Continue") {
HoverObject = "Continue";
}
}
void On$$anonymous$$ouseExit() {
Exit ();
}
public void Hover() {
if (HoverObject.name == "Newgame") {
print ("test");
}
if (HoverObject.name == "Continue") {
print ("test1");
}
}
public void Exit() {
}
}
Answer by Cornelis-de-Jager · Jul 18, 2019 at 10:46 PM
You never define variables for NewGame and Continue. They need to be defined somewhere.
if (GameObject = Newgame) {
HoverObject = "Newgame";
}
if (GameObject = Continue) {
HoverObject = "Continue";
}
It looks like you are assigning a string to a gameobject (that is not right), also in your ifs you want to use == for comparison (since its a gameobject and you probably want to compare against another gameobject). = will do assignment, which you dont want to do in your if statement condition, especially if it will not compile.
@k234234w If you’re comparing two strings, you’d be better off using the .equals() method- the “==“ operator points to memory locations, which is why it works with primitives like ints and things. But if you’re comparing the content of two strings, .equals() is the way to go.
@Code1345 Is he comparing two strings? HoverObject is a GameObject. From his On$$anonymous$$ouseOver it looks like he meant to have Continue and Newgame as GameObjects. HoverObject is defined above as a GameObject. If they are GameObjects then == is fine.
Apologies- I misread his assignment of a “Gameobject” as a regular Object ins$$anonymous$$d. You’re absolutely correct.
@k234234w He assigns a string to HoverObject in his Hover() method I believe- so to compare the two he’d need .equals(), especially since he’s comparing an object to a string literal.
@Code1345 I want you to try assigning a string to a GameObject. It will not compile. he does not need .equals(). Sure, use .equals() if it is a string, but looking at his script it is a GameObject.
Answer by Code1345 · Jul 19, 2019 at 12:28 AM
You have quite a few things wrong with this script, but I’ll try to point them out for you- let me know if this helps. Also, it would be helpful for you to provide the errors you’re getting with your code, and what exactly isn’t working/what you’re trying to accomplish. First of all, you’re trying to compare “Gameobject” with “NewGame” Neither of these objects have been declared/initialized. Same with when you’re comparing “Continue” to a Gameobject. One other thing is if you’re comparing two strings, you should use the .equals method. Let me know if that helps.
its giving me these errors:
Assets/hover.cs(25,20): error CS0103: The name Newgame' does not exist in the current context Assets/hover.cs(25,7): error CS0118: UnityEngine.GameObject' is a type' but a variable' was expected
Assets/hover.cs(10,9): error CS0131: The left-hand side of an assignment must be a variable, a property or an indexer
Assets/hover.cs(26,18): error CS0029: Cannot implicitly convert type string' to UnityEngine.GameObject'
Assets/hover.cs(29,20): error CS0103: The name Continue' does not exist in the current context Assets/hover.cs(29,7): error CS0118: UnityEngine.GameObject' is a type' but a variable' was expected
Assets/hover.cs(30,18): error CS0029: Cannot implicitly convert type string' to UnityEngine.GameObject'
Assets/hover.cs(40,21): error CS0029: Cannot implicitly convert type string' to UnityEngine.GameObject'
Assets/hover.cs(44,21): error CS0029: Cannot implicitly convert type string' to UnityEngine.GameObject'
Your answer
Follow this Question
Related Questions
Not playing entire animation when it is called. 1 Answer
Destroy all previous clones. 2 Answers
Finding all sprite renderers in the scene and tell them to change color 1 Answer
List all objects inside a group 1 Answer
UI Image,UI image 1 Answer