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 maraoz · Dec 05, 2012 at 08:10 PM · bugcursorcursor-customization

Cursor.SetCursor() not working inside Start()

I have this script:

     public Texture2D defaultCursor;
     public Texture2D attackCursor;
 
     void Start() {
         SetCursor(defaultCursor);
     }
 
     public void SetAttackCursor() {
         SetCursor(attackCursor);
     }
 
     public void SetMoveCursor() {
         SetCursor(defaultCursor);
     }
 
     private void SetCursor(Texture2D cursor) {
         Debug.Log("changing state");
         Cursor.SetCursor(cursor, Vector2.zero, CursorMode.Auto);
     }

It works when the functions SetAttackCursor and SetMoveCursor are called from mouse clicking scripts elsewhere. But the cursor is not set at the start of the game when the Start function is called (the debug text is printed but the cursor remains the default cursor from operating system)

Anyone knows why?

Comment
Add comment · Show 3
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 Polymo · Apr 09, 2013 at 06:09 PM 0
Share

For me it's the same, is this 'official behaviour'?

avatar image CostelloNicho · Apr 09, 2013 at 06:23 PM 0
Share

That is strange, this is optimistic but try the Awake() function ins$$anonymous$$d?

avatar image Polymo · Apr 09, 2013 at 06:47 PM 0
Share

i already did that; it's the same, but if i call it manually, like with if(Input.Get$$anonymous$$ey(somekey)) or something similar it works.

3 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by Dracorat · Apr 09, 2013 at 06:55 PM

It's documented under "Web Player" here: (Not that this covers everything - keep reading)

http://docs.unity3d.com/Documentation/ScriptReference/Cursor.html

OSX Webplayer note: Due to sandboxing, Hardware Cursors can only be updated intermittently (when the cursor is moving) the best way to ensure correct behavior if to use OnMouseEnter and OnMouseExit to set the hardware cursors. This is not an issue for software cursors.

It probably has similar restrictions on other platforms.

It's the best I can find.

As an aside, there is a pretty interesting read here: http://blogs.unity3d.com/2012/10/22/cursor-api/

Finally, here's confirmation that someone else who's at least experinced in the field ran in to your problem: http://www.41post.com/5049/programming/unity-assigning-a-texture-to-the-cursor

Therefore, following the MonoBehaviour class logic, if the Cusor.SetCursor() method is called at the Start() or Awake() methods, this should replace the current cursor with the one specified by the Texture2D passed as the first parameter. Although this is the most straightforward approach, it doesn’t work by the time this tutorial is being written.

While he's using a 2 second Invoke to delay and set the cursor, I wonder if you could just use a coroutine with a one frame yield to get the same result.

Sample:

 void Start() {
     StartCoroutine(SetInitialCursor());
 }

 private IEnumerator SetInitialCursor() {
     yield return null;
     SetCursor(defaultCursor);
 }
Comment
Add comment · 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
0

Answer by Sam Bauwens · Apr 09, 2013 at 06:58 PM

Try doing it in the first execution of the Update, or OnGUI function:

 private bool isInitialCursorSet = false;
 
 void Update() {
     if(!isInitialCursorSet)
     {
         isInitialCursorSet = true;
         SetCursor(defaultCursor);
     }
 }
Comment
Add comment · 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
0

Answer by Polymo · Apr 09, 2013 at 08:27 PM

I'm doing it with Invoke and 0.1 sec delay, that indeed works, but feels somewhat superfluous.

Comment
Add comment · 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

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

15 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

Related Questions

cursor appear as a white quare when using hardware cursor 1 Answer

Cursor.SetCursor() not working when building to UWP 0 Answers

HIdden cursor slider issue 1 Answer

Cursor Resize? 1 Answer

Changing Color of Curser 2 Answers


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