Hello I have got a problem I cannot seem to activate functions from one script to another. My understanding of GetComponent doesn’t ever seem to be correct I keep getting the Null Reference to an Object error no matter what I try.
instead of quoting heaps of code I just uploaded the project here, its under scene Level1 the controls are mouse and left click.
when you shoot the enemy ball the score should increase by 1.
can anyone explain to me why this is not working and what I need to do to activate the score function from the enemy death?
Please, just post the script. You’re going to have very few answers if you’re going to ask people to download a whole project, especially if their internet is slow. On my 50/50 internet it will take 5 seconds to download, but for someone with 5mbs it might take 30 seconds just to download. Also I have a good PC which opens projects in seconds, but again, someone might have a bad PC and might take a few minutes to open a project.
It is so much faster if you post code like this (just as reference):
using UnityEngine;
using System.Collections;
public class TESTAnimationControllers : MonoBehaviour {
private Animator Anim;
public PublicAmountHolder PubAmounts;
public bool ChangeAuthBool;
private int AmountChanged;
// Use this for initialization
void Start ()
{
Anim = this.gameObject.GetComponent<Animator>();
PlayerPrefs.SetInt("TESTAnimatorAuthChange", 0);
PlayerPrefs.SetInt("TESTSoldierAnimator", 1);
//count soldiers
PubAmounts.SoldierCount += 1;
}
//HOW-TO
//To change, first authorize:
//PlayerPrefs.SetInt("TESTAnimatorAuthChange", 1);
//then set animation state: PlayerPrefs.SetInt("TESTSoldierAnimator", 1);
// Update is called once per frame
void FixedUpdate ()
{
//let's check if authorized
if (PlayerPrefs.GetInt("TESTAnimatorAuthChange") == 1)
{
//check what state to change to
Anim.SetInteger("SoldierState", PlayerPrefs.GetInt("TESTSoldierAnimator"));
//count how many changed
PubAmounts.AmountChanged += 1;
//now let's see if we're done
CheckIfFinished();
}
if (ChangeAuthBool == true)
{
PlayerPrefs.SetInt("TESTAnimatorAuthChange", 1);
ChangeAuthBool = false;
}
}
void CheckIfFinished()
{
if (PubAmounts.AmountChanged == PubAmounts.SoldierCount)
{
PlayerPrefs.SetInt("TESTAnimatorAuthChange", 0);
//clean up
PubAmounts.AmountChanged = 0;
}
}
}
KABOOM! You just read my script in under 2 seconds with nothing to download or open. Simply use the “Insert” button, select code and copy your script in.
How painful I have to list every game object and what attached to it? I really think it’s easier to click download myself. It’s no wonder I don’t use these help forums very often it’s a very time consuming process
No one wants to download files from random peeps because you don’t know if it’s malicious.
Paste the script in code tags, simpler on everyone as DroidifyDevs demonstrated. It’s not like we need you to throw every script into code tags, just that we need an example. Most Scripting problems have a single source which is why we only need a small part to begin to solve the issue.
With regards to your problem however, if the problem is finding the functions in your script components, you probably didn’t declare the functions you want to access as public.
public void FunctionName()
{
//do stuff
}
If the problem is being unable to grab the script component itself through GetComponent, then the problem I suspect is a naming issue, the class name and file name don’t match or something like that.
What exactly are you trying to do? From you saying Unable to activate functions from other scripts., I’m assuming you need something like:
using UnityEngine;
using System.Collections;
public class ClassToCall : MonoBehaviour {
// Use this for initialization
public void FunctionYouCall ()
{
Debug.Log("It worked");
}
And then
using UnityEngine;
using System.Collections;
public class ClassCallingFrom : MonoBehaviour {
public ClassToCall OtherScript;
// Use this for initialization
void Start ()
{
OtherScript.FunctionyouCall();
}
Oh and as Laparen mentioned, many people don’t want the risk of viruses, especially if they have very important irreplaceable stuff on their PC’s (like me). Just paste the damn scripts and provide a proper explanation. Not that hard…
The 25+ minutes you’ve wasted since creating this thread could have been spent pasting your code. The objects your scripts are attached to is almost irrelevant at this point because you won’t paste the scripts. Not all of us are at a computer that can open a Unity3D project nor do we trust some random person on a public forum.
ok fair well enough I just wished there was an easier way to troubleshoot this.
Well heres the 2 scripts in question.
Scoremanager script attached to Scoremananger object
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class ScoreManager : MonoBehaviour {
public int score;
public Text scoreText;
void Start () {
}
void Update () {
scoreText.text = score.ToString();
}
public void EnemyDeathScore()
{
score = score + 1;
}
}
Enemy1Movement script attached to Enemy1
using UnityEngine;
using System.Collections;
public class EnemyMovement1 : MonoBehaviour {
#region Variables
public Transform target;
public float speed = 1f;
public GameObject EnemyExplosionSpawner;
public GameObject EnemyExplosionClone;
private ScoreManager EnemyDeathScore;
#endregion
#region Basic Functions
void start()
{
EnemyDeathScore = GetComponent<ScoreManager>();
}
void Update()
{
float step = speed * Time.deltaTime;
transform.position = Vector3.MoveTowards(transform.position, target.position, step);
}
#endregion
#region Collision Function
void OnCollisionEnter(Collision other)
{
if (other.gameObject.tag == "Death" || other.gameObject.tag == "Bullet")
{
GameObject Clone_Enemy_Explode_Handler;
Clone_Enemy_Explode_Handler = Instantiate(EnemyExplosionClone, EnemyExplosionSpawner.transform.position, EnemyExplosionSpawner.transform.rotation) as GameObject;
Destroy(Clone_Enemy_Explode_Handler, 5f);
EnemyDeathScore.EnemyDeathScore();
Destroy(this.gameObject);
}
}
#endregion
}
so when I go into game an manually change the player score the score text changes with it fine so that part is working.
but when Enemy1 is hit by bullet object score should increase by 1 but I get null reference error.
You’re trying to get the component from the Enemy1 movement script, but there is NO ScoreManager on the Enemy. You should either make a public ScoreManager and then drag the ScoreManager object onto the slot of the enemy script, or use a GameObject.FindWithTag(“ScoreManager”).GetComponent();
Also, if you’re getting an error, it is helpful to include what line it happened on