Why does my bullet lack a rigidbody even though it has one? - unity3d

So I'm making a FPS game and when I try to shoot, it says my bullet lacks a rigidbody. I have a a rigidbody, so why is it like this?
Here's the bit of code that makes the bullet :
Vector3 aimDir = (transform.forward).normalized;
Instantiate(gunData.bulletPrefab, muzzle.position, Quaternion.LookRotation(aimDir, Vector3.up));
Rigidbody bulletRb = gunData.bulletPrefab.GetComponent<Rigidbody>();
bulletRb.velocity = aimDir * gunData.bulletSpeed;

You seem to be trying to access the bulletPrefab's rigid body component instead of the newly instantiated object's.
try something like this:
var myNewBulletInstance = Instantiate(....
myNewBulletInstance.GetComponent<Rigidbody>();

now It works. I also had to create a prefab variant to put the rigidbody on. thanks!

Related

İn Unity Photon Network , Learn who did the bullet come from?

I make a game using photon network . 2 actors are shooting at each other and when the bullet is formed on the stage I want to know who the bullet came from. I can send player id in bullet Instantiate and I can find player in for loop but i don't think it's true.
Is there better than this method?
Code
void Shoot()
{
var part = GetComponentInChildren<ParticleSystem>();
part.Play();
float angle = cc.isFacingRight ? 0f : 180f;
GameObject gameob = PhotonNetwork.Instantiate("Bullet", firingPoint.position, Quaternion.Euler(new Vector3(0f, 0f, angle)),0, null);
}
You can simply use photonView.Owner, since the bullet is a networked object. Alternatively, you can use photonView.IsMine to check if the client running the check owns the bullet, instead of comparing owners.
(The photonView instance comes from assuming your bullet script extends MonoBehaviourPun instead of MonoBehaviour)

Checking raycast hit on tagged collider

I'm trying to make a shooting game and I wanted to check if an enemy is hit using a raycast. Here is the code:
void CheckForShooting()
{
Vector3 mousePos = Input.mousePosition;
RaycastHit2D bulletCheck = Physics2D.Raycast(gunPoint.position,Camera.main.ScreenToWorldPoint(mousePos), gunRange);
Debug.DrawLine(gunPoint.position,Camera.main.ScreenToWorldPoint(mousePos),Color.white);
if(Input.GetButtonDown("Fire1"))
{
if (bulletCheck.collider.tag =="Enemy")
{
print("Hit");
}
}
}
However, even if the raycast is right on top the red enemy the console doesn't print "Hit" and I get the error "NullReferenceException: Object reference not set to an instance of an object", the line that is getting this error is this one bulletCheck.collider.tag =="Enemy".
Here is a ss:
Screenshot]
You need to make a raycast everytime you click the Fire1
Look at this official unity site: https://learn.unity.com/tutorial/let-s-try-shooting-with-raycasts# its explained how to shoot with raycasts.
I wish i could help you directly but i haven't entered Unity in over a year. I hope this helps but for such simple questions its faster to make a google search in my opinion
You just need to check if collider you were supposed to hit is null or not (if it was actually hit). You shoot the ray and expect it to always hit the target in your current code. Just check:
if(bulletCheck.collider != null)
and the code will only run if there was a hit. Only then you can check what was hit.
Also follow the advice and learn about NullReferenceException, it is the most common and basic exception, also one of the easiest to solve.

How to move an object in Unity

Hello guys I try to make a city building game the idea is very simple actually Instantiate an Building and move them with mouse after that hit the place button and place.
The issue is if the collider of a another building completely encloses the collider of the building that I am built I can't move new building.
I can probably explain better with pictures.
Issue 1
The first picture my new building you can see collider limits and the second one my old building that I already placed. I Understand the problem but I can not solve it.
And here it is my object drag code
private void OnMouseDrag()
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hitInfo;
if (Physics.Raycast(ray, out hitInfo, 1000f,(1 << LayerMask.NameToLayer("Ground"))))
{
Debug.Log(hitInfo.transform.name);
transform.position = SnapToGrid(hitInfo.point);
}
}
private Vector3Int SnapToGrid(Vector3 pos)
{
Vector3 tempPos = pos;
Vector3Int snappedPos;
snappedPos = new Vector3Int(Mathf.RoundToInt(tempPos.x), Mathf.RoundToInt(tempPos.y), Mathf.RoundToInt(tempPos.z));
return snappedPos;
}
Thanks for the help in advance
If I got your question right, I believe that you can solve the problem by enableing the "Is Trigger" option of the previously placed buildings colliders at the moment that a new building is being moved around. Enableing this option in an object makes it so other objects can pass trought it when collision happens.

Unity GetComponent(GUIText) bug?

I have an issue with the GetComponent(GUIText) the error i get is
There is no 'GUIText' attached to the "#######COUNTER(Clone)" game object, but a script is trying to access it.
Here is my code:
var UItecxt = GameObject.Find("#######COUNTER(Clone)");
var txtconvert = UItecxt.GetComponent(GUIText);
print(txtconvert);
txtconvert.text = counternumb.ToString();
I HAVE a GUIText on my clone! What is the issue? Thanks!
Your problem is that there is no GameObject named "#######COUNTER(Clone)" cloned in the scene. Run my code below and you will notice.
var UItecxt = GameObject.Find("#######COUNTER(Clone)");
var txtconvert : GUIText;
if(UItecxt != null)
txtconvert = UItecxt.GetComponent(GUIText);
else
Debug.Log("There was no GameObject with the name '#######COUNTER(Clone)' in the scene");
To fix it just make sure you do have a GameObject with that name.
Your question is not detail enough to determine the problem.
Still, i assumed the gameobject that you mentioned should be a prefab that you instantiated.
Where you put or attach the sciprt? Make sure it is attached to the prefab that you instantiated.
Also, you can try use direct reference the component instead of using gameobject.Find.
It is easier for you to drag and drop element at the inspector.

How to disable a collider when the trigger of another collider has been entered?

I am building a game where the player runs on a path. When the player triggers a collider, 2 enemy objects will spawn.
What I want is when the first collider trigger has been entered, I want the second collider, which is at a certain distance from the first collider, to get disabled for a certain time. How to achieve this?
If you'd like to disable the colliders so they won't hit or rebound off the wall, for example, then you can change your collider's "isTrigger" variable to true, to change it into a trigger volume instead of a solid collider. This has the effect of disabling it - in that it won't cause other objects to stop or rebound if they hit it.
For example:
function Update() {
if (Input.GetKeyDown(KeyCode.X)) {
collider.isTrigger = true;
}
}
Note that things like MouseOver still work.
If you want to disable that completely, you can try collider.enabled = false. I'm not sure if that works or not. If it doesn't, you can always scale down your collider:
var myOldSize:Vector3;
function DisableBoxCollider(myCollider:BoxCollider)
{
//actually just resizes it
myOldSize=myCollider.size;
myCollider.size=Vector3(0,0,0);
}
function EnableBoxCollider(myCollider:BoxCollider)
{
if(myOldSize!=Vector3(0,0,0))
myCollider.size=myOldSize;
}
You can use the above code to integrate it in your own project. I'm not going to spill out all of the code for you because else we'd miss the point of learning to program and post on Stackoverflow in general. But it should help you to get on your way. Try and play some with the code, and if you have questions, get back here and ask them, providing the question with some code to show what you have tried.