Make an object appear during play
I have just finished completing the roll-a-ball tutorial for unity. I am currently having a problem with a GameObject and the count integer. I am attempting to add a little flair by making a ground piece appear upon collecting the first 12 pickups, yet do not know how to reference other scripts. ,I have just finished the learn tutorial for the roll-a-ball game and I am attempting to add a bit of flair to the project. For some reason I cannot place an int, namely count, on a GameObject, and as I am only doing tutorials so far I do not know how to reference other scripts. Once count reaches 12, I would like a cube or GameObject to appear so that the ball can escape the starting arena.
Answer by KittenSnipes · Dec 14, 2017 at 04:41 PM
So this should be your finished script:
// Create public variables for player speed, and for the Text UI game objects
public float speed;
public Text countText;
public Text winText;
private float moveHorizontal;
private float moveVertical;
//This is a reference to the escape route object
public GameObject escapeRoute;
// Create private references to the rigidbody component on the player, and the count of pick up objects picked up so far
private Rigidbody rb;
private int count;
// At the start of the game..
void Start ()
{
// Assign the Rigidbody component to our private rb variable
rb = GetComponent<Rigidbody>();
// Set the count to zero
count = 0;
// Run the SetCountText function to update the UI (see below)
SetCountText ();
// Set the text property of our Win Text UI to an empty string, making the 'You Win' (game over message) blank
winText.text = "";
}
// Each physics step..
void FixedUpdate ()
{
// Set some local float variables equal to the value of our Horizontal and Vertical Inputs
moveHorizontal = Input.GetAxis ("Horizontal");
moveVertical = Input.GetAxis ("Vertical");
// Create a Vector3 variable, and assign X and Z to feature our horizontal and vertical float variables above
Vector3 movement = new Vector3 (moveHorizontal, 0.0f, moveVertical);
// Add a physical force to our Player rigidbody using our 'movement' Vector3 above,
// multiplying it by 'speed' - our public player speed that appears in the inspector
rb.AddForce (movement * speed);
}
public float getHorizontal()
{
return moveHorizontal;
}
public float getVertical()
{
return moveVertical;
}
// When this game object intersects a collider with 'is trigger' checked,
// store a reference to that collider in a variable named 'other'..
void OnTriggerEnter(Collider other)
{
// ..and if the game object we intersect has the tag 'Pick Up' assigned to it..
if (other.gameObject.CompareTag ("Pick Up"))
{
// Make the other game object (the pick up) inactive, to make it disappear
other.gameObject.SetActive (false);
// Add one to the score variable 'count'
count = count + 1;
// Run the 'SetCountText()' function (see below)
SetCountText ();
}
}
// Create a standalone function that can update the 'countText' UI and check if the required amount to win has been achieved
void SetCountText()
{
// Update the text field of our 'countText' variable
countText.text = "Count: " + count.ToString ();
// Check if our 'count' is equal to or exceeded 12
if (count >= 12)
{
// Set the text value of our 'winText'
winText.text = "You Win!";
//Here after we win we enable the escape route to be visible for the player
escapeRoute.SetActive(true);
}
}
You specifically only need this line in the function SetCountText as shown above:
escapeRoute.SetActive(true);
Along with the reference to it at the very top of your code like this:
public GameObject escapeRoute;
Here is a quick video showing the setup:
https://www.dropbox.com/s/4fk31fc6qkpfq90/RollABall.mp4?dl=0
Answer by StormTune19 · Dec 15, 2017 at 03:25 PM
@KittenSnipes Thank you very much for your fast help, however I am still having a problem. I have implimented both pieces of code into PlayerController, i have created a cube named escapeRoute, and under WinText in the unity view of the PlayerController script i have made "Escape Route" bounded to said cube, yet when disabled, even while playing, upon collecting the 12th pick-up nothing happens. I very much appreciate the video help, but i did not see the code in action upon you collecting the 12th pick-up. I also realized that you did not have to bind any object to the script, but I have attempted t do so as no object named escapeRoute would appear if i were to place one.
In addition to this, have some error info. UnassignedReferenceException: The variable escapeRoute of PlayerController has not been assigned. You probably need to assign the escapeRoute variable of the PlayerController script in the inspector. PlayerController.SetCountText () (at Assets/_Completed-Game/Scripts/PlayerController.cs:84) PlayerController.OnTriggerEnter (UnityEngine.Collider other) (at Assets/_Completed-Game/Scripts/PlayerController.cs:68)
I have no idea what any of this means being a beginner and all, so your additional help would be gladly appreciated.
@StormTune19 $$anonymous$$ay I see how you implemented it? I will help in any way I can
I placed both pieces of code in the exact places you have shown up above.
Ok well then you need to place the gameObject which is your escape route in to the assigned spot under player controller. So click on your player and drag it into that empty spot in the script.
If needed I can email you my skype so I can more personally help you. Only if you wish
Don't have skype, this is all i have for now
I will just show you the whole setup via video then
Here is the new video showing what to do with the escape route:
https://www.dropbox.com/s/yg07wfx3fy9jc05/AddingTheEscapeRoute.mp4?dl=0
Your answer
![](https://koobas.hobune.stream/wayback/20220612144602im_/https://answers.unity.com/themes/thub/images/avi.jpg)