Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 ramazanogutlu · Jul 18, 2019 at 10:25 PM · all

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() {

 }

}

Comment
Add comment · Show 1
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 ramazanogutlu · Jul 18, 2019 at 10:28 PM 0
Share

this is the hirkey alt text

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'

capture.png (5.0 kB)

3 Replies

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

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.

Comment
Add comment · Show 2 · 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 ramazanogutlu · Jul 19, 2019 at 07:50 PM 0
Share

yeah i'm just learning and im using the unity forums to help me

avatar image ramazanogutlu · Jul 19, 2019 at 07:55 PM 0
Share

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() {

 }

}

avatar image
0

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";
      }
Comment
Add comment · Show 6 · 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 k234234w · Jul 18, 2019 at 11:18 PM 1
Share

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.

avatar image Code1345 · Jul 19, 2019 at 12:29 AM 1
Share

@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.

avatar image k234234w Code1345 · Jul 19, 2019 at 09:41 AM 0
Share

@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.

avatar image Code1345 k234234w · Jul 19, 2019 at 05:47 PM 0
Share

Apologies- I misread his assignment of a “Gameobject” as a regular Object ins$$anonymous$$d. You’re absolutely correct.

avatar image Code1345 · Jul 19, 2019 at 12:20 PM 0
Share

@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.

avatar image k234234w Code1345 · Jul 19, 2019 at 02:57 PM 0
Share

@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.

avatar image
0

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.

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 ramazanogutlu · Jul 19, 2019 at 07:48 PM 0
Share

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

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

109 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 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 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 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

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


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