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]);
}
}
}
Related
I am trying to access to m_CameraDistance of the CinemachineVirtualCamera, but I get the error "NullReferenceException: Object reference not set to an instance of an object" on the last line of the code.
This is my code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Cinemachine;
public class CameraDistanceController : MonoBehaviour
{
public float _areaCameraDistance = 10;
private float _defaultAreaCameraDistance = 8;
public CinemachineVirtualCamera _vcam;
private CinemachineFramingTransposer _framingTransposer;
void Start()
{
_framingTransposer = _vcam.GetCinemachineComponent<CinemachineFramingTransposer>();
_defaultAreaCameraDistance = _framingTransposer.m_CameraDistance;
}
}
Any ideas? I found another person with the same issue, but there is no answer:
https://quabr.com/69938009/unity3d-cinemachine-how-to-access-camera-distance-in-virtual-camera
Thanks a lot.
I am using _3rdPersonFollow, not CinemachineFramingTransposer. That is why.
See the fix below:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Cinemachine;
public class CameraDistanceController : MonoBehaviour
{
public float _areaCameraDistance = 10;
private float _defaultAreaCameraDistance = 8;
public CinemachineVirtualCamera _vcam;
private Cinemachine3rdPersonFollow _3rdPersonFollow;
void Start()
{
_3rdPersonFollow = _vcam.GetCinemachineComponent<Cinemachine3rdPersonFollow>();
_defaultAreaCameraDistance = _3rdPersonFollow.CameraDistance;
}
}
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.
We are trying to add the ability to lose three child slimes at the same time when attacked by a specific weapon.
Children's slime is managed by List.
List script
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ChildSlimeList : MonoBehaviour
{
[SerializeField]
private List<GameObject> SlimeChild=new List<GameObject>();
public void ChildSlimeRandomOff()
{
for (int i = 0; i < 2; i++)
{
var SlimeNum = Random.Range(0, SlimeChild.Count);
GameObject SlimeChildList = SlimeChild[SlimeNum];
SlimeChildList.SetActive(false);
SlimeChild.RemoveAt(SlimeNum);
}
}
}
Child slime script
using System;
using System.Collections.Generic;
using _SlimeCatch.Stage;
using UnityEngine;
using Random = System.Random;
public class ChildrenSlimeWeaponCollider : MonoBehaviour
{
[SerializeField] private GameObject MolotovCocktail;
public GameObject GameManager;
ChildSlimeList _childSlimeList;
void Start()
{
//_childSlimeList = GetComponent<ChildSlimeList>().SlimeChild();
}
public void OnCollisionEnter2D(Collision2D other)
{
//if (!other.gameObject.CompareTag("Weapon")) return;
//_childSlimeList.SlimeChild.RemoveAt(this.gameObject);
if (other.gameObject.CompareTag("MolotovCocktail"))
{
GameManager.GetComponent<ChildSlimeList>().ChildSlimeRandomOff();
}
Destroy(gameObject);//or SetActive(false)
Destroy(other.gameObject);//or SetActive(false)
}
}
At this rate, the elements of the slime list of the child attacked by a specific weapon will not be deleted directly. Help me.
State of the gameenter image description here
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ChildSlimeList : MonoBehaviour
{
[SerializeField]
private List<GameObject> SlimeChild=new List<GameObject>();
public void ChildSlimeRandomOff()
{
for (int i = 0; i < 2; i++)
{
var SlimeNum = Random.Range(0, SlimeChild.Count);
GameObject SlimeChildList = SlimeChild[SlimeNum];
SlimeChildList.SetActive(false);
SlimeChild.RemoveAt(SlimeNum);
}
}
public void SlimeColliderDecision(GameObject gameObject)
{
SlimeChild.Remove(gameObject);
}
}
using System;
using System.Collections.Generic;
using _SlimeCatch.Stage;
using UnityEngine;
using Random = System.Random;
public class ChildrenSlimeWeaponCollider : MonoBehaviour
{
[SerializeField] private GameObject MolotovCocktail;
public GameObject GameManager;
ChildSlimeList _childSlimeList;
void Start()
{
//_childSlimeList = GetComponent<ChildSlimeList>().SlimeChild();
}
public void OnCollisionEnter2D(Collision2D other)
{
//if (!other.gameObject.CompareTag("Weapon")) return;
if (other.gameObject.CompareTag("MolotovCocktail"))
{
GameManager.GetComponent<ChildSlimeList>().SlimeColliderDecision(this.gameObject);
GameManager.GetComponent<ChildSlimeList>().ChildSlimeRandomOff();
}
gameObject.SetActive(false);
other.gameObject.SetActive(false);
}
}
By making this change, we were able to hide three objects and at the same time reduce the number of elements in the list that contain them.
Many thanks to those who gave me tips.
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;
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;
}
}