Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 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 /
avatar image
2
Question by ConsciousCoder · Jan 07, 2013 at 11:47 PM · c#gameobjectgetcomponent

C# GetComponent / change values throught other script

Hello im trying to access/change the value of a variable in one script through another script which is my SceneManager.cs(lives is declared static in SceneManager.cs). Im having trouble figuring out why? Thanks.

public GameObject sceneManager;

 void OnTriggerEnter (Collider other)
 {
     if(other.gameObject.tag == "Player")
     {
         print("Collided");
         //other.GetComponent.("Player").laserOn = true;
         //other.GetComponent.("Player").bulletOn = false;

         SceneManager sm = gameObject.GetComponent("SceneManager")();
         sm.lives += 1;
         

         Destroy (gameObject);
     }
             
Comment
Add comment · Show 1
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 YoungDeveloper · Feb 12, 2014 at 11:31 AM 2
Share

If anyone is still unsure about getcomponent I explain it clearly in these two topics:

  • http://answers.unity3d.com/questions/550578/cant-understand-getcomponent-c.html

  • http://answers.unity3d.com/questions/597617/how-do-i-addsubtract-variables-between-two-differe.html

3 Replies

· Add your reply
  • Sort: 
avatar image
2

Answer by Valentin_B · Feb 12, 2014 at 11:22 AM

To retrieve a component I almost always use the following syntax:

 //you don't need to access the gameObject reference, it makes you go another unecessary GetComponent call.
 
  SceneManager sm = this.GetComponent<SceneManager>();
 
 

However, if I read your quesiton correctly, you say that "lives" is declared static...so you don't need that anyway, instead of:

 SceneManager sm = gameObject.GetComponent("SceneManager")();
        sm.lives += 1;


Simply do:

 SceneManager.lives += 1;
 
 
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 ConsciousCoder · Feb 27, 2014 at 07:38 PM 0
Share

Thank you for the additionally input Valentin_B. Its been so long i cant find what project i was originally coding this for.

avatar image
0

Answer by DaveA · Jan 07, 2013 at 11:57 PM

Try this:

 SceneManager sm = (SceneManager)gameObject.GetComponent<SceneManager>();
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 ConsciousCoder · Jan 08, 2013 at 12:36 AM

It compiles..but when the condition is met on the OnTriggerEnter method it reads an error "Object is not set to instance of object."

Comment
Add comment · Show 13 · 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 ConsciousCoder · Jan 08, 2013 at 10:51 PM 0
Share

Fixed: for anybody else wondering how to access variables through other scripts($$anonymous$$y script/class is Player.cs):

private Player player;

 void Start ()
 {
     player = (Player)FindObjectOfType(typeof(Player));
 }

     void OnTriggerEnter (Collider other)
     {
 
     if(other.gameObject.tag == "Player")
     {
         
         player.laserOn = true;
         player.bulletOn = false;

         player.lives++;
     }
     }

 
avatar image fafase · Jan 08, 2013 at 11:00 PM 0
Share

Yep, but I think you are heading for issues and head scratching when using this on other cases. FindObjectOfType returns the first active object matching, here it goes fine as you have only one player but that won't do for enemies. You will need GetComponent.

avatar image ConsciousCoder · Jan 09, 2013 at 07:14 AM 0
Share

You know a valid way of using GetComponent because i googled and searched the forums and script refernce and couldn`t find a piece of code that actually worked..

avatar image fafase · Jan 09, 2013 at 07:53 AM 0
Share

There is a detailed article on this here : http://unitygems.com/script-interaction1/

avatar image DanielJF · Jan 09, 2013 at 08:41 AM 1
Share

i learned it myself in a diffrent way that works good for me aswell maybe it helps and it's pretty easy way to access

//Script 1

 using UnityEngine;
 using System.Collections;
 
 public class TestFile1 : $$anonymous$$onoBehaviour {
 
     public int SomeNumber = 1;
     public static TestFile1 main;
     
     
     void Awake () 
     {
         main = this;    
     }
     void Update()
     {
         Debug.Log (SomeNumber);
             //Debug 2
     }
 }
 

//Script 2

 using UnityEngine;
 using System.Collections;
 
 public class TestFile2 : $$anonymous$$onoBehaviour {
 
     // Use this for initialization
     void Start () 
     {
         TestFile1.main.SomeNumber = 2;
     }
 }
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

12 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

Related Questions

C# GetComponent Issue 2 Answers

fix script to get C# var from another object instead of object this script is attached to 2 Answers

insert script question 1 Answer

Accessing GameObjects in my List 1 Answer

how to change gameobject variable to a different gameobject in c# 2 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