Trying to follow the site tutorials, but could use a more well written tutorial then otherScript OtherScript so been trying to do it solo and no matter how I try it, seems to crash and burn.
so on my main player i have a c# script, and in it I have this, but i am mostley pointing/questioning on the score and the addScore function,
using UnityEngine;
using System.Collections;
public class playercontrols : MonoBehaviour {
public float playerSpeed = 15;
public float maxWidth = 25;
public float maxHeight = 4;
public int maxShots = 5;
public int lives = 3;
public int score = 0;
public int level = 0;
public int powerup = 0;
public float powerupLeft = 0;
public GameObject bullet = null;
public Transform bulletSpawn = null;
private float playerCurX = 0;
private float playerCurY = 0;
void Start ()
{
transform.position = new Vector3(playerCurX, 0, 0);
}
void Update ()
{
//See if Player is Moving - change to newtranslations if so
float newTranslationX = Input.GetAxis("Horizontal") * playerSpeed;
newTranslationX *= Time.deltaTime;
playerCurX = playerCurX + newTranslationX;
float newTranslationY = Input.GetAxis("Vertical") * playerSpeed;
newTranslationY *= Time.deltaTime;
playerCurY = playerCurY + newTranslationY;
//Check NEW Translations with max range, if moving outside range set to max
if(playerCurY >= maxHeight)
{ playerCurY = maxHeight; }
if(playerCurY <= -maxHeight)
{ playerCurY = -maxHeight; }
if(playerCurX >= maxWidth)
{ playerCurX = maxWidth; }
if(playerCurX <= -maxWidth)
{ playerCurX = -maxWidth; }
//Set it
transform.position = new Vector3(playerCurX, 0, playerCurY);
//Check to see if shooting
if(Input.GetButtonDown("Jump"))
{
Instantiate(bullet, bulletSpawn.position, bulletSpawn.rotation);
}
}
static void addScore(int adder)
{
score += adder;
}
}
now, on the other objects, im setting it to when they are destroyed, to add a value to the players score, and then in another script remove a object so it will spawn a new one.
the player score function is
static void addScore(int adder)
{
score += adder;
}
The spawner script has a function -
static void subObject()
{
curObjects -= 1;
}
When the target is destroyed, its code is
if(transform.position.y <= -killRange)
{
playerScript = GetComponent("playercontrols") as playercontrols;
playerScript.addScore(10);
spawnscript = GetComponent("spawner") as spawner;
spawnscript.subObject();
Destroy(gameObject);
}
The playercontrols.cs script is attached to the main player, and the script spawner.cs is attached to a empty game object that handels the random spawning.
With this, Unity will either return no errors, but then on running it throw tons of null refrence errors,
or will error with any one of the following…
Assets/Scripts/enemyShip.cs(26,38): error CS0122: `playercontrols.addScore(int)’ is inaccessible due to its protection level
cannot be accessed with an instance reference, qualify it with a type name instead
The error seems to change or just not be caught some times for some reason then just messes up in the game.
Any suggestions on the best way to access variables of other scripts?