i have the following code:
Product code -
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public abstract class Guns : MonoBehaviour //product
{
public abstract void shoot();
public GameObject CreateBullet(GameObject bullet)
{
return Instantiate(bullet, transform.position, Quaternion.identity);
}
public void ShootBullet(GameObject bullet, Vector3 direction)
{
bullet.transform.Translate(direction * Time.deltaTime);
}
}
Concrete Product Code 1
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class M16 : Guns //ConcreteProduct
{
[SerializeField] GameObject m16bullet;
Vector3 m16bulletdirection = new Vector3(5, 0, 0);
public override void shoot()
{
CreateBullet(m16bullet);
ShootBullet(m16bullet, m16bulletdirection);
}
}
Concrete Product Code 2
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Uzi : Guns //ConcreteProduct
{
[SerializeField] GameObject uzibullet;
Vector3 uzibulletdirection = new Vector3(10, 0, 0);
public override void shoot()
{
CreateBullet(uzibullet);
ShootBullet(uzibullet, uzibulletdirection);
}
}
Creator Code
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public abstract class GunCreator : MonoBehaviour //Creator
{
public abstract Guns GunFactory();
}
Concrete Creator Code 1
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class M16Creator : GunCreator // ConcreteCreator
{
public override Guns GunFactory()
{
return new M16();
}
}
Concrete Creator Code 2
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class UziCreator : GunCreator // ConcreteCreator
{
public override Guns GunFactory()
{
return new Uzi();
}
}
and a client code, which currently do nothing as I dont know how to make a gun using this method. So How do all of this help me create new gun prefab with the relevant code on them?
Besides, as far as i understand, every new gun will have to have a NewGunCreator class that derives from GunCreator and a NewGun class that derives from Guns. wouldnt it be easier to leave out the creator classes?
cheers!
Related
I tried to make it so that it will change to the next scene after 3 seconds, but I will only get an error9 (Error photo). I added a code sample.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class ChangeSceneAfterThreeSeconds : MonoBehaviour
{
public void Start()
{
StartCoroutine("LoadSceneThing");
}
public IEnumerator LoadSceneThing(string sceneToChange)
{
yield return new WaitForSeconds(3);
SceneManager.LoadScene(sceneToChange);
}
}
just replace StartCoroutine("LoadSceneThing"); by StartCoroutine(LoadSceneThing("scene name"));
I am making a game where, if the catcher gets destroyed by the object, the game over screen is triggered. All that seems to occur is that there is a giant game over the screen at the beginning of when I play, while the game is running in the background. For some reason, the game does not seem to call in the game over screen only on collision.
This is the script I am using for my catcher, where it collides, disappears, and then the game over screen is set up to be triggered.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CatcherDestroy : MonoBehaviour
{
public GameOverScreen GameOverScreen;
private void OnCollisionEnter2D(Collision2D collision)
{
if (collision.gameObject.CompareTag("Catcher"))
{
Destroy(collision.gameObject);
GameOverScreen;
}
}
}
and this is the code for my GameOverScreen.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class GameOverScreen : MonoBehaviour
{
public Text pointsText;
public void Setup(int score)
{
gameObject.SetActive(true);
pointsText.text = "Score:" + score.ToString();
}
}
You are not calling anything when an object collides, you're just listing the reference of the object. You'll need to call the function that you have exposed. Without calling a method from the script reference, no code will be run. Edit your first snippet as follows:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CatcherDestroy : MonoBehaviour
{
public GameOverScreen GameOverScreen;
private void OnCollisionEnter2D(Collision2D collision)
{
if (collision.gameObject.CompareTag("Catcher"))
{
Destroy(collision.gameObject);
GameOverScreen.Setup();
}
}
}
The other option would be to move the code in Setup to Awake or Start or OnEnable, then instead of calling the function in the collision, you just need to set it as active.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CatcherDestroy : MonoBehaviour
{
public GameOverScreen GameOverScreen;
private void OnCollisionEnter2D(Collision2D collision)
{
if (collision.gameObject.CompareTag("Catcher"))
{
Destroy(collision.gameObject);
GameOverScreen.score = theScore;
GameOverScreen.gameObject.SetActive(true);
}
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class GameOverScreen : MonoBehaviour
{
public Text pointsText;
public int score;
private void OnEnable()
{
pointsText.text = "Score:" + score.ToString();
}
}
The one issue is you'll need to pass in the score parameter which I do not see in your script CatcherDestroy.
I have been following a tutorial for saving player names with playerprefs.
I got it working normal text, but not with TextMeshPro. Is there a way to edit the code so that I can use TextMeshPro with the scripts?
First Script
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class SaveName : MonoBehaviour
{
public InputField textBox;
public void ClickSaveButton()
{
PlayerPrefs.SetString("name", textBox.text);
Debug.Log("Your name is " + PlayerPrefs.GetString("name"));
}
}
Second Script
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class NameScene : MonoBehaviour
{
public Text NameBox;
void Start()
{
NameBox.text = PlayerPrefs.GetString("name");
}
// Update is called once per frame
void Update()
{
}
}
Import TextMeshPro to your script and instead of public Text NameBox; use public TMP_Text NameBox;
I am trying to make a script that will take in a list of animations and automatically add them to the blend tree. I've had a look at the blend tree API and came up with this:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
using UnityEditor.Animations;
public class AnimationSwitcher : MonoBehaviour
{
public List<Motion> animations;
public Animator animator;
public BlendTree blendTree;
public void addMotions()
{
for (int i = 0; i < animations.Count; i++)
{
blendTree.AddChild(animations[i]);
}
}
}
I know that I have to use the animator component, but I can't get a easily readable answer on how I can use it to add the blendtree children. I've tried assigning the blendtree in the editor, but that didn't work either
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
using UnityEditor.Animations;
public class AnimationSwitcher : MonoBehaviour
{
public List<Motion> animations;
public AnimatorController animator;
private BlendTree blendTree;
void Start()
{
animator.CreateBlendTreeInController("Active", out blendTree);
blendTree.AddChild(animations[0]);
}
public void addMotions()
{
for (int i = 0; i < animations.Count; i++)
{
blendTree.AddChild(animations[i]);
}
}
}
Basically, I have this Text (scoreText) which is in my "Menu" scene so hence I have initiated it in GameControlMenu.cs, however, I'm trying to change its text from my other script GameControl.cs whilst I'm currently on my "Main" scene.
GameControlMenu.cs:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
public class GameControlMenu : MonoBehaviour
{
public static GameControlMenu instanceMenu;
public Text scoreText;
void Start()
{
//does stuff but not important to question
}
}
GameControl.cs:
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
public class GameControl : MonoBehaviour
{
public static GameControl instance;
public int score = 5;
void Start()
{
GameControlMenu.instanceMenu.scoreText.text = "PREVIOUS SCORE: " + score;
}
}
This setup works for a couple of my other variables which I'm accessing from another file but for whatever reason this just keeps throwing me the error: NullReferenceException: Object reference not set to an instance of an object
Any help is appreciated :)
You can't do GameControlMenu.instanceMenu... since GameControlMenu is in another scene as you described, and the instance isn't in the current scene.
But what you can do is to store the value somewhere first, and let the GameControlMenu use it when the other scene loads like so:
GameControlMenu.cs
public class GameControlMenu : MonoBehaviour
{
public static GameControlMenu instanceMenu;
public static string StuffToShowOnScoreText { get; set; }
public Text scoreText;
void Awake()
{
// So that it loads the text on start
scoreText.text = StuffToShowOnScoreText;
// ...
}
}
GameControl.cs
public class GameControl : MonoBehaviour
{
public static GameControl instance;
public int score = 5;
void Start()
{
// Store the value
GameControlMenu.StuffToShowOnScoreText = "PREVIOUS SCORE: " + score;
}
}