Regarding SetActive function
In my game . I have added collectables , I have added a collectable naming "PickUp3" . I want it to work such that as the player touches it , the path to Level 2 naming "level2path" becomes active . Here is my script but I don't know why it isn't working . Help please .
Transcript :
using UnityEngine;
using System.Collections;
public class lev2 : MonoBehaviour {
void Start ()
{
}
void OnTriggerEnter(Collider col)
{
if (col.gameObject.name == ("PickUp3"))
{
{
other.gameObject.name==("level2path")
{
other.gameObject.SetActive (true);
}
}
}
}
void Update () {
}
}
Does one of them have a rigidbody attached? Where does the variable 'other' come from in your code?
That code will not even compile because:
if (col.gameObject.name == ("PickUp3")) { { <== Error 1 other.gameObject.name==("level2path") <== Error 2 & Error 3 { other.gameObject.SetActive (true); <== Error 3 } } <== Error 1 }
Error 1: why are there extra brackets without any meaning to them?
Error 2: what is this line?? Did you forget to put this into an if statement?
Error 3: other is not declared anywhere, the script suddenly sees other.gameObject.name
but it never was told what other
is.
I suggest you look at the basic beginner coding tutorials provided by Unity, once you get comfortable writing code, you can try to do your own stuff. Don't start with things you don't understand ;)
Also you seem to misunderstand the OnTrigger functions, they work only for the object the script is applied to. Again, take a step back, learn from the beginning, then gradually improve the complexity of your coding.
You can find the many official tutorials in the Learn Section.
Have nice day.
I thought, let's start with the other variable ;)
Answer by TenebrisMusic · May 12, 2016 at 07:57 PM
There are several things that can be the issue here.
First of all, to what object is this script attached? You would need this script to be on the player for it to pass the if-statement.
Second, does one of the objects (the player or the pickup object) have a rigidbody attached? And have you checked one of the colliders to be a trigger? If not then the OnTriggerEnter method will not be called.
Third, you are calling upon the variable 'other', but you don't seem to have defined it for as far as I can see. Because of this it will not do anything with your object.
If I were you I would assign this script to the pick-up object instead of the player object, and check for a collision with the player to initiate the reaction. This would probably look something like this:
using UnityEngine;
using System.Collections;
public class Pickup3Script : MonoBehaviour {
public GameObject level2path;
void OnTriggerEnter (Collider col) {
if (col.gameObject.tag == "Player") {
level2path.SetActive (true);
}
}
}
In this case you would have tagged your player object with the standard tag "Player", and you would have to drag the level2path object into the inspector. An alternative to having to insert it in the inspector would be to replace
level2path.SetActive (true);
with
GameObject.Find("level2path").SetActive (true);
In this case the path object should be named level2path.
Hope this helps.
Cheers!
Ruben
you are calling upon the variable 'other', but you don't seem to have defined it for as far as I can see. Because of this it will not do anything with your object.
It will not only do nothing with the object, it creates a compiler error which makes it impossible to playtest. ;)
How can I define the variable "other" . Only that's the main problem
http://unity3d.com/learn/tutorials/modules/beginner/scripting/variables-and-functions?playlist=17117
Like I said, watch the beginner scripting tutorials from 1-28 that should clear all your starting problems.
If you don't know how to declare a new variable yet, there is no sense in continuing, do you understand? ;) Learn the basics, then try advanced stuff. It's not hard, the tutorials are a very good source of information.
Like Le Pampelmuse said, it would be wise to first follow the basic tutorials. It will spare you a lot of time in the end as you won't have to turn to the community and wait for answers that can be found rather easily.
There are a great deal of tutorials on the official Unity Tutorial page you'll find useful. I'd recommend following some of the scripting tutorials first, but I - as a sometimes impatient person myself - can understand you just want to begin your game. I'd try finding a tutorial that gets closest to your game-concept, so you'll be able to continue on what you've build for the tutorial when you feel like you understand enough.
Cheers!
"It will spare you a lot of time in the end as you won't have to turn to the community and wait for answers that can be found rather easily."
$$anonymous$$ore importantly, it is not the purpose of UA to explain things that are publicly available to read. ;)