Why is the GUI components obsolete? - c#

I'm currently making a remake of Dreadhalls in Unity3D. I got the code but I got 2 errors:
Assets\Standard Assets\Utility\ForcedReset.cs(6,27): error CS0619: 'GUITexture' is obsolete: 'GUITexture has been removed. Use UI.Image instead.'
Assets\Standard Assets\Utility\SimpleActivatorMenu.cs(10,16): error CS0619: 'GUIText' is obsolete: 'GUIText has been removed. Use UI.Text instead.'
I tried replacing them with the items given to me in the error, but it is still getting errors saying that the namespacee name UI is not found. I'm still not used to Unity so I need some help.
Here is the code of ForcedReset.cs:
using System;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityStandardAssets.CrossPlatformInput;
[RequireComponent(typeof (GUITexture))]
public class ForcedReset : MonoBehaviour
{
private void Update()
{
// if we have forced a reset ...
if (CrossPlatformInputManager.GetButtonDown("ResetObject"))
{
//... reload the scene
SceneManager.LoadScene(SceneManager.GetSceneAt(0).name);
}
}
}
And here is the code of SimpleActivatorMenu.cs:
using System;
using UnityEngine;
namespace UnityStandardAssets.Utility
{
public class SimpleActivatorMenu : MonoBehaviour
{
// An incredibly simple menu which, when given references
// to gameobjects in the scene
public GUIText camSwitchButton;
public GameObject[] objects;
private int m_CurrentActiveObject;
private void OnEnable()
{
// active object starts from first in array
m_CurrentActiveObject = 0;
camSwitchButton.text = objects[m_CurrentActiveObject].name;
}
public void NextCamera()
{
int nextactiveobject = m_CurrentActiveObject + 1 >= objects.Length ? 0 : m_CurrentActiveObject + 1;
for (int i = 0; i < objects.Length; i++)
{
objects[i].SetActive(i == nextactiveobject);
}
m_CurrentActiveObject = nextactiveobject;
camSwitchButton.text = objects[m_CurrentActiveObject].name;
}
}
}
Is there a problem?

GUIText and Other Elements have been Removed from the Newer Versions of Unity Due to Combability and Some Other Issues!
Fix for GUI Text
Make Sure that you Import the Following Line of Statement in your Code:-
using UnityEngine.UI;
Then, Simply Change from
public GUIText camSwitchButton;
to
public Text camSwitchButton;
Fixed for GUI Texture
This Fix Gave Me Some Warning on Other Files But Worked Properly! It Might Be Fixed in the Upcoming Updates of Unity!
Make Sure that you Import the Following Line of Statement in your Code:-
using UnityEngine.UI;
Then, Change from this Line of Code
[RequireComponent(typeof (GUITexture))]
to,
[RequireComponent(typeof(Texture))]
Hope So this Helps! I Have Checked it and it works Fine But Just Gave Me Some Warnings which I Don't Care!
But It will be Fine and will be Fixed in Some Upcoming Versions of Unity!

Fixed
For the ForcedReset.cs file change its contents with the following code:
using System;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
using UnityStandardAssets.CrossPlatformInput;
[RequireComponent(typeof(Texture))]
public class ForcedReset : MonoBehaviour
{
private void Update()
{
// if we have forced a reset ...
if (CrossPlatformInputManager.GetButtonDown("ResetObject"))
{
//... reload the scene
SceneManager.LoadScene(SceneManager.GetSceneAt(0).name);
}
}
}
For the SimpleActivatorMenu.cs file change its contents with the following:
using System;
using UnityEngine;
using UnityEngine.UI;
namespace UnityStandardAssets.Utility
{
public class SimpleActivatorMenu : MonoBehaviour
{
// An incredibly simple menu which, when given references
// to gameobjects in the scene
public Text camSwitchButton;
public GameObject[] objects;
private int m_CurrentActiveObject;
private void OnEnable()
{
// active object starts from first in array
m_CurrentActiveObject = 0;
camSwitchButton.text = objects[m_CurrentActiveObject].name;
}
public void NextCamera()
{
int nextactiveobject = m_CurrentActiveObject + 1 >= objects.Length ? 0 : m_CurrentActiveObject + 1;
for (int i = 0; i < objects.Length; i++)
{
objects[i].SetActive(i == nextactiveobject);
}
m_CurrentActiveObject = nextactiveobject;
camSwitchButton.text = objects[m_CurrentActiveObject].name;
}
}
}

Related

How to load death scene in C#/unitygames?

haven't posted on here before but I have been trying for a while to create a game and would like a death/game over sort of scene to appear when the player loses all 3 of their lives. I have a functioning game manager and my player can lose lives (they have 3). This is all being done in unity games and is 2d (idk if that helps). I currently have other stuff in my scene loader script that works fine so I will post the whole thing but I am having issues with the bottom most code!
Thank you!
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class SceneLoader : MonoBehaviour
{
public string scenename;
public GameManager GM;
private void OnTriggerEnter2D(Collider2D collision)
{
if(collision.tag == "Player")
{
SceneManager.LoadScene(scenename);
}
}
private void Deathscene()
{
if(GM.LifeTotal == 0)
{
SceneManager.LoadScene(Bob);
}
}
}
Gamemanager script
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class GameManager : MonoBehaviour
{
public int PotionsCollected = 0;
public int LifeTotal = 3;
public Text PotionsOutput;
public Text LifeOutput;
void Update()
{
PotionsOutput.text = "Potions: " + PotionsCollected;
LifeOutput.text = "Life: " + LifeTotal;
}
public void CollectPotion()
{
PotionsCollected++;
}
public void UsePotion()
{
PotionsCollected--;
}
public void LoseLife()
{
LifeTotal--;
}
}
What you can do is from your Unity Editor go to File->Build Settings and then drag and drop inside the active scenes window your death scene.
Then an index will be generated on the right side of the window and you can use that index to load the scene. like this:
SceneManager.LoadScene("Use your generated index");
NOTE: Use your index as a number and not as a string.
UPDATED Solution:
public void LoseLife()
{
LifeTotal--;
if(LifeTotal <= 0)
{
SceneManager.LoadScene("Use your generated index");
}
}
I supposse LoseLife() it's called when the enemy attacks your player.

How to make object appear when a certain torso is called using C# in Unity

This is the code I used for a tutorial
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Trial_Changer : MonoBehaviour
{
[Header("Sprite To Change")]
public SpriteRenderer bodyPart;
[Header("Sprites to Cycle Through")]
public List<Sprite> options = new List<Sprite>();
public static int currentTorsoOption = 0;
public void NextOption ()
{
currentTorsoOption++;
if (currentTorsoOption >= options.Count)
{ currentTorsoOption = 0; }
bodyPart.sprite = options[currentTorsoOption];
}
public void PreviousOption()
{
currentTorsoOption--;
if (currentTorsoOption <= 0)
{ currentTorsoOption = options.Count - 1; }
bodyPart.sprite = options[currentTorsoOption];
}
}
**This is the code I am using to try to say "if torso 2 is called, create object"
**
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class SpeechAppears : MonoBehaviour
{
public GameObject speechBubbleTOSpawn;
private int Torso = Trial_Changer.currentTorsoOption;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
if (Torso == 2)
{
Instantiate(speechBubbleTOSpawn);
}
}
}
Except the torso just appears always.
What do I do to fix this?
I want an object to appear when torso 2 is on the screen but instead the object is just always present. What do I do ?
The culprit is this line :
private int Torso = Trial_Changer.currentTorsoOption;
Since you assign value Torso only once at the start of the game, its value always be the same.
Try this :
private int Torso => Trial_Changer.currentTorsoOption;
This change Torso from a field to a property, and when Torso is used, it will read the current value of Trial_Changer.currentTorsoOption.

the script don't inherit a native class that can manage a script unity

I'm a beginner programmer on c# and Unity.
So I was making a grid system for my game and when I'm trying to add as a component every script in my unity project it prints this one error : "The script don't inherit a native class that can manage a script"
I don't know what i need to do.Help me please if u can
//////////first script///////////////
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Gird1 : MonoBehaviour
{
private int width;
private int height;
private int[,] gridArray;
public Gird(int width, int height)
{
this.width = width;
this.height = height;
gridArray = new int[width, height];
Debug.Log(width + " " + height);
}
}
////////second script////////////////////
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Testing : MonoBehaviour
{
private void Start()
{
Grid grid = new Grid(20, 10);
}
void Update()
{
}
}
image of grid
Your first script is named "Gird" not "Grid".
Second Script:
public class Testing : MonoBehaviour
{
public Gird1 gird1; // Assign in the inspector. We need an instance of Gird1
private void Start()
{
if (gird1 == null) { // In case someone forgot to assign in the inspector
gird1 = (Gird1)FindObjectOfType(typeof(Gird1)); // .. We'll search the scene
}
gird1.Gird(20,10); // Call the Gird() function on your gird1 instance
}
}

Setting levels in a quiz game

This is my first post to Stack Overflow so please be gentle. I have followed the video tutorial at https://learn.unity.com/tutorial/live-session-quiz-game-1 and have been able to successfully modify it so that images are shown as questions instead of text. The next step for me is to divide my game into Levels. I have added the appropriate extra 'Rounds' in the DataController object referred to at the end of video 3 start of video 4 so that it now looks like this:
So here is the question, if I want to add a set of buttons to a Levels page, and then add an OnClick event to each, how do I point that OnClick event specifically to the set of questions for each Level? I think I need to point the onClick at a script passing a variable that is the level number, but not sure how to do it and so far searches of StackOverflow and YouTube haven't really helped?
EDIT
I have done the following and I seem to be a lot closer:
Created a new scene called LevelSelect and added the buttons I need for the levels to it;
I created a script in the scene called LevelSelectController and to this I added the button registration script provided by Lothan;
I registered the buttons as suggested via drag and drop;
I created a Function within the LevelSelectController script called StartGame, this had one line:
Debug.Log("Button pressed is: Level " + (setLevel + 1));
I ran the script and the buttons responded as expected;
All I am struggling with now his how to get the button press to pass the integer for the level number to the allRoundData variable in the DataController. The code for each script looks like this:
DataController
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
public class DataController : MonoBehaviour
{
public RoundData[] allRoundData;
public int currentLevel;
// Start is called before the first frame update
void Start()
{
DontDestroyOnLoad (gameObject);
SceneManager.LoadScene ("MenuScreen");
}
public RoundData GetCurrentRoundData()
{
return allRoundData [currentLevel];
}
// Update is called once per frame
void Update()
{
}
}
LevelSelectController
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
public class LevelSelectController : MonoBehaviour
{
public List<Button> buttonsList = new List<Button>();
private DataController dataController;
public void StartGame(int setLevel)
{
Debug.Log("Button pressed is: Level " + (setLevel + 1));
}
}
GameController
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
public class GameController : MonoBehaviour
{
public Text questionText;
public Image questionImage;
public SimpleObjectPool answerButtonObjectPool;
public Transform answerButtonParent;
private DataController dataController;
private RoundData currentRoundData;
private QuestionData[] questionPool;
private bool isRoundactive;
private float timeRemaining;
private int questionIndex;
private int playerScore;
private List<GameObject> answerButtonGameObjects = new List<GameObject> ();
// Start is called before the first frame update
void Start()
{
dataController = FindObjectOfType<DataController> ();
currentRoundData = dataController.GetCurrentRoundData ();
questionPool = currentRoundData.questions;
timeRemaining = currentRoundData.timeLimitInSeconds;
playerScore = 0;
questionIndex = 0;
ShowQuestion ();
isRoundactive = true;
}
private void ShowQuestion()
{
RemoveAnswerButtons ();
QuestionData questionData = questionPool[questionIndex];
questionText.text = questionData.questionText;
questionImage.transform.gameObject.SetActive(true);
questionImage.sprite = questionData.questionImage;
for (int i = 0; i < questionData.answers.Length; i++)
{
GameObject answerButtonGameObject = answerButtonObjectPool.GetObject();
answerButtonGameObjects.Add(answerButtonGameObject);
answerButtonGameObject.transform.SetParent(answerButtonParent);
AnswerButton answerButton = answerButtonGameObject.GetComponent<AnswerButton>();
answerButton.Setup(questionData.answers[i]);
}
}
private void RemoveAnswerButtons()
{
while (answerButtonGameObjects.Count > 0)
{
answerButtonObjectPool.ReturnObject(answerButtonGameObjects[0]);
answerButtonGameObjects.RemoveAt(0);
}
}
// Update is called once per frame
void Update()
{
}
}
How do I now pass the value of setLevel in LevelSelectController to currentLevel in the DataController script?
Welcome to StackOverflow!
You can do it using different ways, but one could be:
First register all the buttons:
List<Button> buttonsList = new List<Button>();
Then assign to each button the behaviour to do when onClick (registering the listener) passing the info of the corresponding DataController:
for(int i = 0; i < buttonsList.Count; i++)
{
buttonsList[i].onClick.AddListener(() => SetLevel(DataController.allRoundData[i]))
}
There are some leaps of faith in this current answer cause I don't know about your code, but if you have doubts, comment and I'll update the answer ^^
With a little extra research I was able to use a static variable along with Lothan's suggestion to get to the solution. Here is the modified code:
LevelSelectController
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
public class LevelSelectController : MonoBehaviour
{
static public int currentLevel
public List<Button> buttonsList = new List<Button>();
void Start()
{
for(int i = 0; i < buttonsList.Count; i++)
{
int levelNum = i;
buttonsList[i].onClick.AddListener(() => {currentLevel = levelNum;});
}
}
public void StartLevel()
{
SceneManager.LoadScene ("Game");
}
}
And only one edit needed in the Start() of GameController.cs, from
currentRoundData = dataController.GetCurrentRoundData ();
to:
currentRoundData = dataController.GetCurrentRoundData (LevelSelectController.currentLevel);

Why if I'm setting a int variable value to 0 in the top of a script in the inspector the value is 1?

using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using UnityEditor;
using UnityEngine;
public class DialogueTrigger : MonoBehaviour
{
public List<Conversation> conversations = new List<Conversation>();
public int dialogueNum = 0;
public int dialogueIndex = 0;
After doing Build > Rebuild Solution
dialogueIndex value in the Inspector is 0 but the value of dialogueNum is 1
I never changed it's value in the Inspector. ( Maybe I did and forgot ? )
How can I make sure that the value will be 0 in the Inspector when running the game ? The problem is that before making it public it was hidden so I couldn't see that it's value is 1 in the inspector :
[HideInInspector]
public int dialogueNum = 0;
[HideInInspector]
public int dialogueIndex = 0;
Now I see that it's value is 1
Do initializations in Awake() method and assignments in Start(). This is why these methods made for. So your script should be like:
public List<Conversation> conversations;
public int dialogueNum;
public int dialogueIndex;
void Awake(){
conversations = new List<Conversation>();
}
void Start(){
dialogueNum = 0;
dialogueIndex = 0;
}
Now the values will be assigned every time you run the game as you wanted.

Categories