Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
avatar image
0
Question by Fisherrr · May 06, 2015 at 08:19 PM · c#passingscript reference

How to pass float from one script to another in Unity 5? C#

I'm working in Unity 5 Personal edition.. I'm trying to pass float from one script to another, but I get this error:

Assets/CameraTracksPlayer.cs(15,36): error CS1061: Type ClimberMovement' > does not contain a definition for > stopCamera' and no extension method stopCamera' of type ClimberMovement' could be found (are you missing a using directive or an assembly reference?)

I don't know what am I doing wrong.

Here is my code:

 using UnityEngine;
 using System.Collections;
 
 public class CameraTracksPlayer : MonoBehaviour {
 
     Transform player;
     float isDead;
     float offsetY;
 
     // Use this for initialization
     void Start () {
 
         GameObject go = GameObject.Find ("MainCamera");
         ClimberMovement dying = go.GetComponent <ClimberMovement> ();
         float died = dying.stopCamera; //this line gets error!
         isDead = died;
 
 
         GameObject player_go = GameObject.FindGameObjectsWithTag("Player")[0];
 
         if (player_go == null) {
             Debug.LogError("Ne dela ker ni taga Player");
             return;
         }
         player = player_go.transform;
 
         offsetY = transform.position.y - player.position.y;
 
     }
     
     // Update is called once per frame
     void Update () {
         if (player != null && isDead == 1) {
             Vector3 pos = transform.position;
         }
         else if (player != null) {
             Vector3 pos = transform.position;
             pos.y = player.position.y + offsetY;
             transform.position = pos;
         }
 
     }
 
 }

This is the code of the script from which I am trying to pass variable:

 using UnityEngine;
 using System.Collections;
 
 public class ClimberMovement : MonoBehaviour {
 
     ...
 
     public float stopCamera = 0f;
 
     ...
 
         if (transform.position.x <= -3.9f) {
             transform.position = new Vector3 (-3.9f, transform.position.y, transform.position.z);
             if (transform.position.x == -3.9f) {
                 dead = true;
                 stopCamera = 1f;
                 animator.SetTrigger ("Death");
                 velocity.y = -5f;
                 velocity.x = -5f;
             }
         } else if (transform.position.x >= -1f) {
             transform.position = new Vector3 (-1f, transform.position.y, transform.position.z);
         }
 
 
     }
 
 }
Comment
Add comment · Show 2
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 tanoshimi · Jun 06, 2015 at 06:35 AM 0
Share

Your error message looks a little odd: it has some ">" characters in odd places: > does not contain a definition for > stopCamera'

Are you sure you haven't got a typo in your original code somewhere - perhaps got too many > on line 14?

avatar image Mouton · Jun 06, 2015 at 08:57 AM 0
Share

You should change

 GameObject go = GameObject.Find ("$$anonymous$$ainCamera");

by

 GameObject go = Camera.main;

I suspect the "stopCamera" is inside a Sub-Class that's why the compiler can't see it.

2 Replies

· Add your reply
  • Sort: 
avatar image
-2

Answer by Fruhlicht · Jun 06, 2015 at 06:28 AM

You can only pass static vars, so change public float stopCamera = 0f; into public static float stopCamera = 0f;

Comment
Add comment · Show 2 · 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 tanoshimi · Jun 06, 2015 at 06:33 AM 1
Share

This is just nonsense. https://unity3d.com/learn/tutorials/modules/beginner/scripting/getcomponent

avatar image Fornoreason1000 · Jun 06, 2015 at 06:51 AM 0
Share

tanoshimi is correct statics are bad if you are using them that way. static members tend to linger in memory because GC doesn't know when they are going to be referenced next(even if they are NO instances of the class!). thus they are only removed from memory(yes even long after using Object.Destroy) when your game stops running.

Another thing is that static member will be always the same on all instances because static members do not rely the instance. e.g if staticVar = 6, and you have 3 class instances that have that same variables and you change it to 7 on one of them, they will all be 7.

static memebers are best used for constant or global members that wikll be needed throughout the entire game. e.g you could have a static instance of a "SystemData" class that will track the game time, unlock or settings and be able to accessed from various UIs.

to access normal instance variables of a class, find an instance of that class(see tanoshimis link) or create one with the new keyword (if it inherits from $$anonymous$$onoBehaviour use AddComponent)

avatar image
0

Answer by Ilgiz · Jun 05, 2015 at 01:53 PM

Try to write your ClimberMovement code like this:

  using UnityEngine;
  using System.Collections;
  
  public class ClimberMovement : MonoBehaviour {
      public float stopCamera = 0f;
      }

 
Comment
Add comment · 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

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Passing More Then One Int Into Another Script As A GUI 0 Answers

Making a bubble level (not a game but work tool) 1 Answer

An OS design issue: File types associated with their appropriate programs 1 Answer


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