cinemachine Error on trying to access Camera Distance in virtual camera - c#

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;
}
}

Related

Implementing Factory Method design pattern in Unity

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!

I want to delete the list element at the same time that the game object in the list disappears

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.

Unity add children to blendtree

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]);
}
}
}

Unity Changing the Text of an object in another script/scene

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;
}
}

Type `int' does not contain a definition for `Text' and no extension method `Text' of type `int' could be found

Im getting this error in unity, my problem is highscore, it doesnt work. I tried all kinds of videos on youtube but i think im doing something wrong in all of them. Also tried with Text highscore and also doesnt work. It says to me " Are you missing an assembly reference?"
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class ScoreScript : MonoBehaviour {
public static int scoreValue = 0;
public Text score;
public static int highscore;
void Start () {
highscore.Text = PlayerPrefs.GetInt("HighScore", 0);
score = GetComponent<Text>();
scoreValue = 0;
}
void Update () {
score.text = "" + scoreValue;
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class hazardCollisionFunctions : MonoBehaviour {
private void OnCollisionEnter(Collision col){
if(col.gameObject.tag == "Platform"){
this.gameObject.SetActive(false);
ScoreScript.scoreValue += 1;
}
if(col.gameObject.tag == "Player"){
if(ScoreScript.highscore < ScoreScript.scoreValue){
PlayerPrefs.SetInt("HighScore", ScoreScript.scoreValue);
}
SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
}
}
}
Add a public Text highScoreText; field to your ScoreScript so ScoreScript can work with it.
Set highScoreText.Text to "" + highScore in Start so the text gets set appropriately.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class ScoreScript : MonoBehaviour {
public static int scoreValue = 0;
public Text score;
public Text highScoreText;
public static int highscore;
void Start () {
highscore = PlayerPrefs.GetInt("HighScore", 0);
highScoreText.Text = "" + highScore;
score = GetComponent<Text>();
scoreValue = 0;
}
void Update () {
score.text = "" + scoreValue;
}
}
Drag the high score text object into the highScoreText field of the ScoreScript script in the inspector

Categories

Resources