Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 /
This question was closed Apr 20, 2012 at 08:39 PM by TheDarkVoid for the following reason:

The question is answered, right answer was accepted

avatar image
2
Question by TheDarkVoid · Apr 18, 2012 at 08:11 PM · gameobjectgetcomponentcomponentsingleton

Acessing a Script Instance from Another Object

I have this script:

 using UnityEngine;
 using System.Collections;
 
 public class Menu_Options : MonoBehaviour 
 {
     //Vars
     public Material GUI_N;
     public Material GUI_O;
     public static bool Active = false;
     public Transform Menu;
     public GameObject Cam;
     //Functions
     void OnMouseEnter()
     {
         renderer.material = GUI_O;
         Active = true;
     }
     void OnMouseExit()
     {
         renderer.material = GUI_N;
         Active = false;
     }
     void Update()
     {
         if(Active)
         {
             if(Input.GetMouseButtonUp(0))
             {
                 Options();
                 Debug.Log("Options");
             }
         }
     }
     void Options()
     {
         var s = Cam.GetComponent<SmoothCam>();
         s.Instance._SetTarget(Menu);
     }
 }

But i get this error:

 Assets/Scripts/Menu_Options.cs(37,19): error CS0176: Static member `SmoothCam.Instance' cannot be accessed with an instance reference, qualify it with a type name instead

This is how the other script is set up:

 using UnityEngine;
 using System.Collections;
 
 public class SmoothCam : MonoBehaviour {
 
     public static SmoothCam Instance;
     
     void Awake()
     {
         Instance = this;
     }
     
     public void _SetTarget(Transform t)
     {
         target = t;
     }
     ---The rest is not important---

What does "qualify it with a type name" mean, and how can i fix or work around this?

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

3 Replies

  • Sort: 
avatar image
6
Best Answer

Answer by Kmulla · Apr 18, 2012 at 08:35 PM

Try something like this:

 public class Menu_Options : MonoBehaviour {
     private SmoothCam SmoothCamScript;

     void Awake() {
         SmoothCamScript = Cam.GetComponent<SmoothCam>();
     }
     ...
     SmoothCamScript._SetTarget(Menu);

I've never tried anything with an 'instance' in the other script, but this method usually works for me :)

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 TheDarkVoid · Apr 18, 2012 at 08:44 PM 0
Share

yay, that works perfect, i usually use instance when it's on the same object. thank you so much for your help.

avatar image Tseng · Apr 18, 2012 at 08:47 PM 0
Share

The "Instance" thing you used was actually called singleton pattern (though, its more of a hybrid, but similar to it). Actually this pattern should be used used only in certain situations (Game$$anonymous$$anager for example).

Static variables are accessed via $$anonymous$$yClass.myStaticVariable w/o needing an instance of the script

avatar image
1

Answer by Lttldude · Apr 18, 2012 at 08:30 PM

I think

 var s = Cam.GetComponent<SmoothCam>();

should become

 SmoothCam s = Cam.GetComponent<SmoothCam>();

because it defines the type s is.

Then if you want to run some function or get some variable off of that script use it like this

 s._SetTarget(transform);

Hope this helps.

Comment
Add comment · Show 1 · 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 TheDarkVoid · Apr 18, 2012 at 08:37 PM 0
Share

i'm trying to run the _SetTarget function located in the SmoothCam script from within the $$anonymous$$enu_Options script. that didn't work.

avatar image
0

Answer by Tseng · Apr 18, 2012 at 08:45 PM

Your "Instance" variable is static, you don't need to obtain an instance to access it

 void Options() {
    var s = Cam.GetComponent<SmoothCam>();
    s.Instance._SetTarget(Menu);
 }

Just do

 SmoothCam.Instance._SetTarget(Menu);
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

Follow this Question

Answers Answers and Comments

8 People are following this question.

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

Related Questions

Weird GetComponent error 2 Answers

What is the difference between GameObject.GetComponent and Component.GetComponent? 1 Answer

Accessing script variables of instantiaded prefab 1 Answer

Unable to get component from a gameobject 0 Answers

How to get a component from an object and add it to another? (Copy components at runtime) 12 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