Turn on Gravity on current object - c#

I am working on a simple game - when I click on the screen a cube should start using gravity (I have unchecked the "Use Gravity" setting for that cube). However, I do not know how to reference it. I found that I could write .useGravity = true; but I have no idea what to put before the dot.

Gravity comes from the RigidBody. So use something like this in the code.
GetComponent<RigidBody>.useGravity=true;
This of course assumes your script is acting on the same object. If you need to get another object, well, that's a different question.

You can give him a special name like "CubePlayer" and after in your code reference him as:
GameObject.Find("CubePlayer").use....
Other way is to assign a var to it from Inspector, in JavaScript it is like this:
public var MyCube : GameObject;
...
MyCube.use.....
It is possible in C to do this too.

Related

How to assign variables in the inspector for a script on an instantiated object?

Good evening.
So I want to assign a text box to an instantiated object but how do I go about doing this via code and not manually. As hundreds of these prefabs could be instantiated I need to be able to assign the text box in the inspector via code at runtime (As in when they are created).
Just to note: all these fields are assigned on the object that this prefab is a clone of, just for some reason the scripts copy over but the assigned values don't?
Let me know if you need to see any code but I think the question explains enough.
Thank you in advance!
https://gamedev.stackexchange.com/questions/135209/how-to-store-references-to-scene-objects-in-prefabs - This post answers my question. (It is a set of ways to assgn variabkes to prefabs in the inspector).
I thought I might as well repost it here as the answer is on a different forum.
GameObject bullet = Instantiate(bulletPrefab, transform.position, transform.rotation);
bullet.GetComponent<BulletScript>().shooter = gameObject;
Seems to be the best way to do it, using GetComponent to decidd what script you want to assign something to and then assigning that part using .variable = whatever you want it to be.

Stopping the character from moving

So I'm using the unity standard assets pack and I was trying to figure a way to freeze the players location, at first I tried to freeze the players location using the rigidbody but the character was still able to walk. Next I tried to disable the script that allows the player to move but I cannot see the script is other scripts (when I use public script name variable name it doesn't show up as a script name.
Since I can’t see the script, I don’t have much to go off of, but I’m assuming you’re using some either Input.Horizontal and Input.Vertical, or Input.getKey() to do your things.
If you want to freeze the movement, you can simply wrap the function for movement that you are using in an if statement with some kind of bool that contains whether or not the player should be frozen.
Example:
If(!frozen){
Player.Position.x += 5;
}
Or something like that.
If you need to get the variable in another script you could do something like this (this is a rough example to try to explain it, syntax may not be perfect):
GameObject player;
Player.MoveScript.frozen = false;
Something like that.

Spawning gameobjects relative to the position, width and length of another gameobject?

I'm currently developing a game in Unity using C# and I've run into a small problem.
I need to spawn a certain gameobjects relative to the Spawnposition and length of another game object. Now I figured that bounds.size would be the best function to use in this instance. As shown bellow I declare first the variable that uses this in my start method:
public void Start()
{
GameObject PointBar = (GameObject) Resources.Load("PointBar Horizontal");
PointBarVectorLength = PointBar.GetComponent<BoxCollider2D>().bounds.size.x;
PointBarVectorConv = Camera.main.WorldToViewportPoint(new Vector2(PointBarVectorLength, 0f));
}
However, the gameobjects in question are inactive at start's call and thus I presume don't return any value for bounds.size when called.
Does anyone know how I can access the bounds.size or equivalent of an inactive gameobject or is there something else I'm missing here?
As noted in the documentation for the Collider.bounds property, bounds "will be an empty bounding box if the collider is disabled or the game object is inactive". So your assumption was pretty much right. Since the object doesn't exist in worldspace that makes sense.
I'm not sure about the most elegant solution for your use case but two options spring to mind.
Calculate the object's spawn dimensions by accessing its transform.localScale and factoring in the scale of prospective parent objects. That could get messy but you could probably also write a helper method to make it more manageable.
Instantiate the object somewhere off screen after you load it, access its Collider.bounds property to get the information you need then move it where ever you want it or store the information for later.
There may be better solutions but that's what leaps to mind for me.
I solved the issue by using GetComponent().bounds.size.x;
instead of BoxCollider. This component can get accessed when the game object is not active.

Method "AddForce()" isn't working on my object

I tried to add force on the object called 'simple banana' but it isn't working. There is my code :
simpleBanana.GetComponent<Rigidbody>().AddForce(Vector3.back * 10000f, ForceMode.Impulse);
Insert "Debug.Log (" ... ");" on the next line works. The mass of the object is 1, the object is not kinematic
First and most important: this way you are getting rigidbody will cause low performance.
GetComponent is a very expensive method, never do it on Update, do it in void Setup, saving it to an object of RigidBody type, like in the API reference:
https://docs.unity3d.com/ScriptReference/Rigidbody.AddForce.html
Second:
I think it can be a reference problem.
I had a lot of trouble with it when I was starting.
Be sure simpleBanana is the object which contains the RigidBody.
If your reference to that RigidBody is not dragging it and dropping it, be sure to navigate at simpleBanana before applying GetComponent.
Like if your script in on simplePotato, and you trying to access simpleBanana, do something like this:
RigidBody simpleBanana = GameObject.Find('simpleBanana').GetComponent<RigidBody>();
or if simpleBanana is child of simplePotato:
RigidBody simpleBanana = this.transform.GetChild(index_of_the_child).GetComponent<RigidBody>();
After getting the reference right, you can add whatever force you want.
But remember to get components only on void Setup.

Hierarchy Climbing and Descending Unity

I have the following hierarchy on my Player prefab for what will be a very simple multiplayer shooter.
It works this way, the Controller object has the scripts that deal with player input, the PlayerShip object has all of the turning, moving, shooting scripts etc and the Camera is just as it sounds, a camera with a few scripts on it for moving it about.
When a new Player is instantiated the Control script needs to locate its relevant PlayerShip, simple enough.
This can be achieved using the following code:
_playerShip = gameObject.transform.parent.gameObject.transform.FindChild("PlayerShip").GetComponent<PlayerShip>();
Which works fine, the only thing is, to me that looks very clunky, inelegant and brittle. Consequently, I'm wondering if there's a better, more efficient, less ugly way of achieving the same thing?
First of all,
_playerShip = gameObject.transform.parent.gameObject.transform.FindChild("playerShip").GetComponent<PlayerShip>();
is redundant. That can be reduced to
_playerShip = gameObject.transform.parent.FindChild("playerShip").GetComponent<PlayerShip>();
or even use '/' just like you do with folder names.
_playerShip = GameObject.Find("Player/playerShip").GetComponent<PlayerShip>();
Now, instead of instantiating the Object and searching for it later on, you can actually instantiate it and retrieve the reference at the-same time.
GameObject obj = Instantiate(prefab,Vector3.zero,Quaternion.identity) as GameObject;
_playerShip = obj.GetComponent<PlayerShip>();
If it is a network game that you instantiate with Network.Instantiate:
GameObject obj = Network.Instantiate(prefab,Vector3.zero,Quaternion.identity,0) as GameObject;
_playerShip = obj.GetComponent<PlayerShip>();
Now, send the PlayerShip reference to the Control script.
If this isn't being called every frame consider this:
_playerShip = GameObject.Find("PlayerShip").GetComponent>PlayerShip>();
Read more about the above here: https://docs.unity3d.com/ScriptReference/GameObject.Find.html
If it is being called every frame look in to this:
https://docs.unity3d.com/ScriptReference/GameObject.FindWithTag.html
Either one should be easier to understand.
EDIT: I may have misunderstood. Would rearranging the hierarchy so that controller is a parent of playerShip? Then you should be able to just look at the child, rather than going through a parent to a sibling.

Categories