Access Variables From 1st Script to 2nd Script? (C#)
Hi, I am making a 2D game, and I need some help on how to access variables. I am trying to have the camera stop moving when a player collides with an enemy from a separate script, and the problem isn't the collision, it's the ability to access a variable from another script that I am having a problem with. I tried searching how but nothing really worked. Here is my script:
using UnityEngine;
using System.Collections;
using UnityEngine.SceneManagement;
public class TheRestarter : MonoBehaviour {
public MoveCamera moveCamera;
private Animator anim;
public string GameOverScene;
void Awake () {
anim = GetComponent<Animator> ();
MoveCamera moveCamera = MainCamera.GetComponent<MoveCamera>(); Error ---> "The name 'MainCamera' does not exist in the current context"
moveCamera.speed = 0;
}
void OnCollisionEnter2D(Collision2D target)
{
if(target.gameObject.tag == "Enemy")
{
anim.Play ("RedGiantDeath");
StartCoroutine(DelayedSceneLoad());
}
}
IEnumerator DelayedSceneLoad()
{
yield return new WaitForSeconds(1.5f);
SceneManager.LoadScene(GameOverScene);
}
}
On that script I try to access the "speed" variable from this script:
using UnityEngine;
using System.Collections;
public class MoveCamera : MonoBehaviour {
public float speed = 0.5f;
public float maxSpeed = 0.5f;
void Start () {
}
// Update is called once per frame
void Update () {
Vector3 temp = transform.position;
temp.x += speed * Time.deltaTime;
transform.position = temp;
speed += 0.5f * Time.deltaTime;
if (speed < maxSpeed)
speed = maxSpeed;
}
}
As you can see I have an error saying "The name 'MainCamera' does not exist in the current context." Could someone please help me?
Answer by Jessespike · Jun 22, 2016 at 09:55 PM
MoveCamera moveCamera = Camera.main.GetComponent<MoveCamera>();
@Jessespike The camera still doesn't stop when the player collides with the enemy.
Look at the $$anonymous$$oveCamera code. If you set speed to 0, then speed will equal maxSpeed. Is that intended?
if (speed < maxSpeed)
speed = maxSpeed;
Also look over the enemy collision code. It does nothing regarding camera, If you want the camera to stop, shouldn't it be disabling $$anonymous$$oveCamera or at least be setting speed to 0?
@Jessespike I partly got it to work. (with your help :) I took away the speed equals max speed part of the code, thank you for that. This is my new enemy collision code:
using UnityEngine; using System.Collections; using UnityEngine.Scene$$anonymous$$anagement;
public class TheRestarter : $$anonymous$$onoBehaviour { public $$anonymous$$oveCamera moveCamera; private Animator anim; public string GameOverScene; public GameObject Player;
void Awake () {
anim = GetComponent<Animator> ();
Player = GameObject.FindWithTag("Player");
}
void OnCollisionEnter2D(Collision2D target)
{
if(target.gameObject.tag == "Enemy")
{
moveCamera.speed = 0;
anim.Play ("RedGiantDeath");
StartCoroutine(DelayedSceneLoad());
}
}
IEnumerator DelayedSceneLoad()
{
yield return new WaitForSeconds(1.5f);
Scene$$anonymous$$anager.LoadScene(GameOverScene);
}
}
I added movecamera.speed = 0; line of code, thank you for that also.
Just one problem. When my player collides with an enemy the camera speed starts over, because when you first start the game, the camera gradually get's faster. So when the player collides with an enemy, the camera speed gets slow, but is still moving.
Your answer
Follow this Question
Related Questions
How can I access to child objects variables from other C# script ? 0 Answers
assign a new value to a variable 0 Answers
Script error and no Variables showing 0 Answers
How to connect two c# scripts and connect varibles? 1 Answer
How to compare two different variables in two different scripts? 0 Answers