Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by Ejpj123 · Jun 22, 2016 at 08:43 PM · script.variablesaccessfirstsecond

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?

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by Jessespike · Jun 22, 2016 at 09:55 PM

 MoveCamera moveCamera = Camera.main.GetComponent<MoveCamera>();

Script Reference - Camera.main

Comment
Add comment · Show 5 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Ejpj123 · Jun 23, 2016 at 07:42 PM 0
Share

@Jessespike The camera still doesn't stop when the player collides with the enemy.

avatar image Jessespike · Jun 23, 2016 at 08:12 PM 0
Share

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?

avatar image Ejpj123 Jessespike · Jun 23, 2016 at 11:54 PM 0
Share

@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.

avatar image Ejpj123 Ejpj123 · Jun 24, 2016 at 03:05 PM 0
Share

@JesseSpike

Show more comments

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

60 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges