- Home /
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);
}
If anyone is still unsure about getcomponent I explain it clearly in these two topics:
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;
Thank you for the additionally input Valentin_B. Its been so long i cant find what project i was originally coding this for.
Answer by DaveA · Jan 07, 2013 at 11:57 PM
Try this:
SceneManager sm = (SceneManager)gameObject.GetComponent<SceneManager>();
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."
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++;
}
}
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.
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..
There is a detailed article on this here : http://unitygems.com/script-interaction1/
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;
}
}