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 jedimar · Aug 22, 2015 at 08:37 PM · classescalling

call a class in another class

i am a beginner in unity and c#, as well as oop, i am since two days to study for this proble but i cannot find the solution, and das problem: there is a class which has just start and update:using UnityEngine; using System.Collections; public class White : MonoBehaviour { public float duration = 1.0F; public Light lt; void Start() { lt = GetComponent(); } void Update() { if(Input.GetKeyDown(KeyCode.W)) {

         float phi = Time.time / duration * 2 * Mathf.PI;
         float amplitude = Mathf.Cos(phi) * 3F + 3F;
         lt.intensity = amplitude;}}}

i need to call this funtion, which is for adjusting the light in another class, with a if statment such as: public White whitelight;//there is in this script too //if the ray cast, then call the function light adjusting , i must stop here? if (Physics.Raycast (ray, out hits, 100f)) {

         if (hits.collider.tag == "Light") {
             //Debug.Log ("Raycast hit a light");
                                   ????????

how i can call this function, because the first script has just state and update method, i dont know, how ? or must i write the code again under if statment? Thank you very much for your response, please note i ma absolute beginner, if you want to answer for beginner please, thank you again.

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

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by Lord_Ford · Aug 23, 2015 at 12:18 AM

To call a class in another class you can

1: Make an instance of the class you wish to call

 public class SOME_CLASS : MonoBehaviour {
 
 public static SOME_CLASS instance;
 
 public void Move()
 {
 do something...
 }
 }

Then from there you can simply call it

 public class MY_CLASS : MonoBehaviour {
 
 void Start()
 {
 SOME_CLASS.instance.Move();
 }
 }

2: The other way would be to reference the class like:

 public class CLASS : MonoBehaviour {
 
 public SOME_CLASS someclass;
 
 void Start()
 {
 someclass.Move();
 }
 }

Cheers @jedimar! -PlayWell Studios

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
avatar image
0

Answer by jedimar · Aug 23, 2015 at 04:12 PM

Hi, thank you very much for your post, i have tried both option, i have deleted Update, and rewrite it as public void Move(), as well as instance, is there something wrong that i do not have the Move() for calling in other class, i mean, it is not blue as you have, thank you again, you do not know how much you help me with your respond, i appreciate your advice. Thank you.

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 Lord_Ford · Aug 23, 2015 at 04:20 PM 0
Share

Hello and welcome. You can call your function whatever you want (dosent have to be $$anonymous$$ove), so long as it is within the set rules. If you can post your script and I will try to help you out. Cheers!

avatar image jedimar · Aug 24, 2015 at 10:30 AM 0
Share

Hi, i want to know, if a class has just Start and Update, without any other method on it, how i can call it in other class, as i see, the call with instance, it is based on a $$anonymous$$ethod, but my class has just start and update, and i need call it in other class, my class is: using UnityEngine; using System.Collections;

public class Radio : $$anonymous$$onoBehaviour {

 public float duration = 3.0F;
 public AudioSource audio;
 public static Radio instance;

 void Start() {
     audio= GetComponent<AudioSource>();
     audio.Play();

     float phi = Time.time / duration * 5 * $$anonymous$$athf.PI;
     Volum = $$anonymous$$athf.Cos (phi) * 0.5 + 0.5;
     audio.volume = 0.0f; 
     audio.Play();
 }

 void Update(){
 }
 public void $$anonymous$$ove() {

     //if(Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.R)) {
     audio= GetComponent<AudioSource>();
     audio.Play();

     float phi = Time.time / duration * 5 * $$anonymous$$athf.PI;
     float amplitude = $$anonymous$$athf.Cos (phi) * 0.5F + 0.5F;
     //audio.volume = amplitude; 
    
          
 }
 }  

Thank you very $$anonymous$$uch,

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Use another Class in a script with 'Using..' 0 Answers

Call a method of a class derived from MonoBehaviour from a class that isn't. 1 Answer

Is it possible to customize how a variable of a certain type would look in the inspector? 1 Answer

Array of unique inherited classes? 0 Answers

Confused as hell about static classes and stuff. 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