Problem with 2D GUITexture

Hello,

I have been following a Unity tutorial from the book Unity3x Game Development Essentials by Will Goldstone. At one point I must create a GUITexture and attach to it a semi-transparent png texture made in Photoshop. At first I read that in Unity 5 we no longer can create GUITexture the same way and UI->Image would do it. That didn’t work, because I couldn’t attach a texture to the source image.

Then I tried GameObject->Create Empty->Add Component->GUITexture and then add the texture to the empty field. That didnt work either as I added the texture, but never saw it appear on screen.

Afterwards I tried adding a GUITexture to the aforementioned image, but that failed as well.

Any ideas how to add the texture in order to see it on screen? It is supposed to be a model of an empty battery that should be seen in 2D by the player at all times and which, when gathering power cells, should change its appearance to full (there are 4 more images for that). I attached the files, but in case you can’t access them - this is where you can get all the assets: Search | Packt Subscription The asset name is “hud_nocharge”.

Thank you. Have a great day!

3174899--241959--hud_charge4.png
3174899--241960--hud_charge3.png
3174899--241961--hud_charge2.png
3174899--241962--hud_charge1.png
3174899--241963--hud_nocharge.png

Yes, unfortunately, that book is leading you down an abandoned path. You need to skip that chapter (or however much of the book is talking about the old UI stuff).

Instead go work through some of the official UI tutorials. The new UI system is very easy once you grasp a few basics. To make a battery, you will just throw a Canvas and an Image into your scene, and set the texture of the Image (which must be a Sprite in this case, rather than a Texture2D). You can even easily do things like fill the battery to any arbitrary amount of charge, which was really hard to do in the old UI system.

1 Like

Today I got another error I cannot fix - reffering to a guiText object. I checked out the tutorials and none of them explain how to reference a UI object in Javascript. That’s exactly my problem, I get how to work with them in unity, but I can’t reference them in the code correctly, so I end up not being able to drag objects into public variables in script components. And abandoning this book is pretty much not an option, because it’s the first project I have to create at work, so any advices how to make the old tutorial on unity 5 are appreciated.

Thank you and have an awesome day!

Oh dear — it’s got you using Javascript (actually Unityscript) too? That’s unfortunate, as the community has almost entirely given up on it, and even Unity has recently dropped news that they’re expecting to deprecate it soon (see thread here). C# is the language any new code should be written in.

However, that’s not what you want to hear, nor is it probably very helpful.

One idea, if you really have to stick with this book, would be to go to the Unity Download Archive and get an older version of Unity that matches what the book is teaching. You can still download versions back to 3.x there. Then everything should actually work as described.

Otherwise, yes, you really should get rid of your guiText and use a Text object (in the UnityEngine.UI namespace) instead. The reason you can’t refer to a guiText object is that there is no such thing. In the old system, GUI elements were not objects; they were drawn, in immediate mode, by commands in your code. Thus there was nothing for other scripts to refer to. (The solution in that situation is to instead get a reference to the script that’s doing the drawing, and provide public interfaces on that which other scripts can manipulate.)

The thing is I have another unity project successfully created on this version. And when I show my work I would have to switch between Unity3x and latest Unity version. I can install Unity 3x as well without the two versions interfering, correct? If I was to import my Unity3x Game Development Essentials project from latest Unity version to 3x, I would lose everything I have managed to get done so far that has even small differences between the two version, correct? Like, they have changed one thing and my terrain is gone?

Also, I’m creating this project in order to learn as much as possible about Unity, so I could eventually create my own projects with it. So, learning from an old version is probably not the best way, because I would have to learn the new things later on when working.

Since they are wondering about how to use Unity with JS in the future, maybe I should tell them about those news? Is it certain or just a proposition?

Thank you and have a nice day!

You can have multiple versions of Unity installed, no problem. (Just rename the old Unity folder before you run the installer, so it doesn’t install right over it.)

If you take your project from Unity2017 or whatever and try to open it in Unity 3x, it’s going to have a bit of a fit. All of your scripts and simple assets will be fine, but the actual scene files may not open (I don’t know for sure, as I’ve never tried it). So, you might have to redo some of that. The terrain itself will still be usable, I expect, as that’s a fairly simple asset and I’m hopeful they haven’t changed its file format in a long time.

Right. This is why I’m encouraging you to abandon OnGUI, abandon Javascript, and honestly, abandon any books you have lying around that were written for Unity 3.x. That stuff is old. Sure, much of it is the same, but anything dealing with sprites (2D games) or UI is completely obsolete. You’re learning the wrong things. If your goal is to learn as much as possible about Unity, it’s just wasting your time.

It sounds pretty certain to me. They’re only working out the details of how to help people make the transition from JavaScript to C#. There are tools that can make that relatively painless, but still, the more JavaScript you have, the more pain there’s going to be.

Can you send me a link to official Unity statements about abandoning JS? I couldn’t find any.

Since this project is half done, I think I would try to create it as well as possible on this version of Unity, it’s a test project, so it doesn’t have to be perfect. I will just tell my boss some of the stuff are obsolete and it isn’t exactly what it’s supposed to be.

So, where would you recommend me to learn working with Unity 2017?

Thank you.

The thread already linked here is all there is. They haven’t made an “official” announcement yet, but there is a clear statement there from a Unity employee. Believe it or deny it, makes no difference to me.

Here.

Yeah… the only reason I never used those tutorials is that they are mostly in C# and I had to write in JS. Maybe I tell my boss that unofficially JS won’t be supported by Unity and he should consider switching to C#. There are no major game engines that you can write in JS anymore, correct? I read something along those lines on the forums here.

Thank you for the help

I’m not aware of any, though I haven’t really looked. Keep in mind that UnityScript is not actually JavaScript; it’s only JavaScript-like, but is neither a superset nor a subset of actual JS.

Of course if you want to do HTML5/Canvas games, JS is your go-to language, and there are probably JS frameworks around to support you in that. But for native desktop games… yeah, it’s pretty much C# or C++. (Plus for iOS only, you could use ObjC or Swift; and for Android, you could use Java. But I’d recommend Unity/C# over all of those.)

C# is nothing to fear; it’s easier in most ways than JavaScript, not just because it’s a cleaner language that was actually designed rather than evolved way beyond its original purpose, but also because the compiler can catch many errors at compile time that, in JS, can only be caught at runtime. I’ve heard people who have made the switch (in Unity) say it only took them a couple of days to feel comfortable with C#, even though they’d never used it before.

I think you have a pretty good argument against UnityScript when you show your boss the latest Unity 2017 editor. There is no option to create anything but C# scripts anymore. I don’t know if it even needs to be stated more officially than that.

UnityScript has been Milton’d (and hopefully no buildings are at risk of being set on fire).

I would love to program in C#, it’s just that I was told to do so in JS, because we are already working with JS here.

Hey, what do you know? The official announcement is finally out.

RIP US

Thank you for all the help.

1 Like